From 64d9dcd649a0abff8d1004c5ce42f4fe1e1ba014 Mon Sep 17 00:00:00 2001 From: aperron Date: Sun, 12 Dec 2021 18:52:13 +0100 Subject: [PATCH] add exclude filter on input arg #46 --- README.md | 3 ++- src/importer.ts | 18 +++++++++--------- src/test/test1.config.js | 3 ++- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index ae87eac..8099211 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,8 @@ sts -c .stsconfig.js Strapi folder(s)/file(s) with models *.settings.json You may define multiple inputs. In case your API models have relations to other plugins like 'users-permissions'. `sts path/to/strapi/api/ path/to/strapi/plugins/users-permissions/models -o path/to/your/types/dir/` -Order matters, if you have two models with the same name, the last one is used. + * Order matters, if you have two models with the same name, the last one is used. + * Add '!' to exclude folder or subfolder, ex: `!path/to/strapi/plugins_excluded`. * **-g components** Strapi folder(s) with components models diff --git a/src/importer.ts b/src/importer.ts index 014787a..5778450 100644 --- a/src/importer.ts +++ b/src/importer.ts @@ -54,9 +54,8 @@ const walk = ( }); }; -export const findFiles = (dir: string, ext: RegExp = /.settings.json$/) => +export const findFiles = (dir: string, ext: RegExp = /.settings.json$/, exclude: string[] = []) => new Promise((resolve, reject) => { - const filter = (f: string) => ext.test(f); walk( dir, (err, files) => { @@ -66,7 +65,7 @@ export const findFiles = (dir: string, ext: RegExp = /.settings.json$/) => resolve(files); } }, - filter + (f: string) => ext.test(f) && !exclude.map(f => path.resolve(f)).find(x => f.startsWith(x)) ); }); @@ -76,9 +75,10 @@ export const findFiles = (dir: string, ext: RegExp = /.settings.json$/) => * */ export async function findFilesFromMultipleDirectories(...files: string[]): Promise { - const inputs = [... new Set(files)] + const exclude = files.filter(f => f.startsWith("!")).map(f => f.replace(/^!/, '')) + const inputs = [... new Set(files.filter(f => !f.startsWith("!")))] - var actions = inputs.map(i => fs.statSync(i).isFile() ? [i] : findFiles(i)); // run the function over all items + var actions = inputs.map(i => fs.statSync(i).isFile() ? [i] : findFiles(i, /.settings.json$/, exclude)); // run the function over all items // we now have a promises array and we want to wait for it @@ -90,21 +90,21 @@ export async function findFilesFromMultipleDirectories(...files: string[]): Prom /* */ -export const importFiles = (files: string[], results:IStrapiModel[] = [], merge: Partial = {}) => +export const importFiles = (files: string[], results: IStrapiModel[] = [], merge: Partial = {}) => new Promise((resolve, reject) => { let pending = files.length; - if(files.length === 0) resolve(results); + if (files.length === 0) resolve(results); files.forEach(f => { try { const data = fs.readFileSync(f, { encoding: 'utf8' }); - + pending--; let strapiModel = Object.assign(JSON.parse(data), { _filename: f, ...merge }) if (strapiModel.info && strapiModel.info.name) { - + let sameNameIndex = results.map(s => s.info.name).indexOf(strapiModel.info.name); if (sameNameIndex === -1) { results.push(strapiModel); diff --git a/src/test/test1.config.js b/src/test/test1.config.js index 2ae1689..74d5222 100644 --- a/src/test/test1.config.js +++ b/src/test/test1.config.js @@ -4,7 +4,8 @@ const config = { input: [ 'src/test/api/', - 'src/test/strapi/' + 'src/test/strapi/', + '!src/test/strapi/strapi-plugin-users-permissions' ], components: 'src/test/components/content/', output: 'src/test/out1/',