-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c0e9ad
commit 5f799be
Showing
13 changed files
with
132 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,74 @@ | ||
import { StoryTemplateOptions } from '../types'; | ||
import path from 'path'; | ||
import { loadConfig, extractDocuments } from '@component-controls/config'; | ||
import { loadStore } from '@component-controls/store'; | ||
import { StoryTemplateOptions, TeplateFormats } from '../types'; | ||
import { CliOptions } from './types'; | ||
import { saveTemplate } from './save-template'; | ||
import { createStoriesTemplate } from '../stories-template'; | ||
|
||
export const cliStory = (options: CliOptions): void => { | ||
const formatExtensions: { [key in TeplateFormats]: string } = { | ||
cjs: '.js', | ||
esm: '.js', | ||
ts: '.ts', | ||
}; | ||
|
||
export const formatExtension = ( | ||
fileName: string, | ||
format: TeplateFormats, | ||
): string => { | ||
const basename = path.basename(fileName, path.extname(fileName)); | ||
const extension = formatExtensions[format]; | ||
return path.join(path.dirname(fileName), basename + extension); | ||
}; | ||
|
||
export const cliStory = async (options: CliOptions): Promise<void> => { | ||
const { | ||
renderer, | ||
format, | ||
overwrite, | ||
config, | ||
test = 'stories.test.js', | ||
config = '.config', | ||
test, | ||
bundle, | ||
name, | ||
output = 'tests', | ||
output, | ||
} = options; | ||
const storyPath = ''; | ||
return saveTemplate<StoryTemplateOptions>( | ||
{ | ||
renderer, | ||
format, | ||
overwrite, | ||
config, | ||
test, | ||
bundle, | ||
name, | ||
output, | ||
storyPath, | ||
}, | ||
createStoriesTemplate, | ||
); | ||
let documents: string[] = []; | ||
|
||
if (bundle) { | ||
const store = loadStore(require(bundle)); | ||
documents = Object.keys(store.docs).map(key => store.docs[key].title); | ||
} else { | ||
const configPath = path.resolve(process.cwd(), config); | ||
const configuration = loadConfig(configPath); | ||
if (configuration) { | ||
documents = | ||
extractDocuments({ config: configuration.config, configPath }) || []; | ||
} | ||
} | ||
for (const name of documents) { | ||
const basename = path.basename(name); | ||
const splitName = basename.split('.'); | ||
const componentName = splitName[0]; | ||
const fileFormat = | ||
format || path.extname(name).startsWith('.ts') ? 'ts' : 'esm'; | ||
const testName = | ||
test || formatExtension(`${componentName}.test.js`, fileFormat); | ||
await saveTemplate<StoryTemplateOptions>( | ||
{ | ||
renderer, | ||
format, | ||
overwrite, | ||
config, | ||
test: testName, | ||
bundle, | ||
name: options.name || componentName, | ||
storyPath: name, | ||
output: output | ||
? output | ||
: bundle | ||
? path.dirname(bundle) | ||
: path.dirname(name), | ||
}, | ||
createStoriesTemplate, | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters