From 2961d6ee7dc924a03b2edc2efe2b49d02b23b05d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Lins?= Date: Mon, 22 Feb 2021 09:15:18 -0300 Subject: [PATCH] Inserting valid extensions option --- README.md | 16 +++--- src/cli.ts | 7 +-- src/generator.ts | 127 ++++++++++++++++++++++++----------------------- src/types.ts | 1 + 4 files changed, 79 insertions(+), 72 deletions(-) diff --git a/README.md b/README.md index ccfbf3e..7ec4f81 100644 --- a/README.md +++ b/README.md @@ -74,9 +74,10 @@ export default [ ### Options -| Option | Type | Description | -| ------ | ------ | --------------- | -| output | string | App output file | +| Option | Type | Description | Default | +| ------------ | ---------- | ------------------------ | ------------------------ | +| `output` | `string` | Declarations output file | The value of `pkg.types` | +| `extensions` | `string[]` | Valid Extensions | .svelte, .ts, .js | ## Using with cli @@ -86,10 +87,11 @@ svelte-dts -i src/index.ts -o dist/index.d.ts ### Options -| Option | Alias | Description | -| ------------------------------ | --------------- | --------------- | -| --input [input] | -i | App input file | -| --output [output] | -o | App output file | +| Option | Alias | Description | +| -------------------------------------- | --------------- | ------------------------ | +| --input [input] | -i | App input file | +| --output [output] | -o | Declarations output file | +| --extensions [extensions] | -e | Valid Extensions | ## NPM Statistics diff --git a/src/cli.ts b/src/cli.ts index 1386a3c..2eca6f6 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -11,13 +11,14 @@ const exec = async (): Promise => { .name(`svelte-dts`) .version(packageJson.version, '-v --version', 'Version number') .helpOption('-h --help', 'For more information') - .requiredOption('-i, --input ', 'dts input') - .requiredOption('-o, --output ', 'dts output') + .requiredOption('-i, --input ', 'input of application') + .requiredOption('-o, --output ', 'output of declarations') + .option('-e, --extensions ', 'valid extensions') .parse(process.argv); const options = program.opts(); - const generator = new Generator(options.input, { output: options.output }); + const generator = new Generator(options.input, { output: options.output, extensions: options.extensions }); await generator.read(); await generator.write(); }; diff --git a/src/generator.ts b/src/generator.ts index 2f03652..106a03a 100644 --- a/src/generator.ts +++ b/src/generator.ts @@ -15,85 +15,88 @@ class Generator { private dir: string; private output: string; private input: string; - private options: Options; + private extensions: string[]; constructor(input: string, options: Options) { - this.options = options; - this.packageJson = require(path.join(process.cwd(), 'package.json')); this.input = path.join(process.cwd(), input); this.dir = path.dirname(this.input); - this.output = this.options.output || this.packageJson.types; + this.output = options.output || this.packageJson.types; + this.extensions = options.extensions || ['.svelte', '.ts', '.js']; } async read(): Promise { const files: string[] = await readdir(this.dir, ['node_modules']); + await Promise.all(files.map((item) => this.readFile(item))); + } - for (let i = 0; i < files.length; i++) { - const filename = files[i]; - const pathParser = path.parse(filename); - const extension = path.extname(filename); + async readFile(filename: string): Promise { + const pathParser = path.parse(filename); + const extension = path.extname(filename); - if (pathParser.base.includes('.test') || pathParser.base.includes('.spec')) { - continue; - } + if (pathParser.base.includes('.test') || pathParser.base.includes('.spec')) { + return; + } + + if (!this.extensions.includes(pathParser.ext)) { + return; + } - if (extension === '.svelte') { - const fileContent = await fs.readFile(filename, { encoding: 'utf-8' }); - - let scriptTsContent: string = ''; - const resultPreprocess = await preprocess( - fileContent, - [ - { - script: ({ content, attributes }) => { - if (attributes.lang === 'ts') { - scriptTsContent = content; - - const resultTranspile = ts.transpileModule(content, { - compilerOptions: { - module: ts.ModuleKind.ESNext, - target: ts.ScriptTarget.ESNext, - moduleResolution: ts.ModuleResolutionKind.NodeJs, - strict: true, - }, - }); - - return { code: resultTranspile.outputText }; - } - - return { code: content }; - }, + if (extension === '.svelte') { + const fileContent = await fs.readFile(filename, { encoding: 'utf-8' }); + + let scriptTsContent: string = ''; + const resultPreprocess = await preprocess( + fileContent, + [ + { + script: ({ content, attributes }) => { + if (attributes.lang === 'ts') { + scriptTsContent = content; + + const resultTranspile = ts.transpileModule(content, { + compilerOptions: { + module: ts.ModuleKind.ESNext, + target: ts.ScriptTarget.ESNext, + moduleResolution: ts.ModuleResolutionKind.NodeJs, + strict: true, + }, + }); + + return { code: resultTranspile.outputText }; + } + + return { code: content }; }, - ], - { filename } - ); + }, + ], + { filename } + ); + + if (scriptTsContent) { + const compiled = svelteCompile(resultPreprocess.code, { + filename, + }); - if (scriptTsContent) { - const compiled = svelteCompile(resultPreprocess.code, { - filename, - }); - - this.transformers.push( - new SvelteTransformer( - scriptTsContent, - filename, - compiled.ast, - this.dir, - this.packageJson.name, - this.input === filename - ) - ); - } - } else if (extension === '.ts') { - this.transformers.push( - new TypescriptTransformer(filename, this.dir, this.packageJson.name, this.input === filename) - ); - } else if (extension === '.js') { this.transformers.push( - new JavascriptTransformer(filename, this.dir, this.packageJson.name, this.input === filename) + new SvelteTransformer( + scriptTsContent, + filename, + compiled.ast, + this.dir, + this.packageJson.name, + this.input === filename + ) ); } + } else if (extension === '.ts') { + this.transformers.push( + new TypescriptTransformer(filename, this.dir, this.packageJson.name, this.input === filename) + ); + } else if (extension === '.js') { + this.transformers.push( + new JavascriptTransformer(filename, this.dir, this.packageJson.name, this.input === filename) + ); } } diff --git a/src/types.ts b/src/types.ts index f3b0599..a140a1e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,6 @@ export type Options = { output: string; + extensions?: string[]; }; export type Prop = {