Skip to content

Commit

Permalink
add exclude filter on input arg
Browse files Browse the repository at this point in the history
  • Loading branch information
aperron committed Dec 12, 2021
1 parent 795be80 commit 64d9dcd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions src/importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]>((resolve, reject) => {
const filter = (f: string) => ext.test(f);
walk(
dir,
(err, files) => {
Expand All @@ -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))
);
});

Expand All @@ -76,9 +75,10 @@ export const findFiles = (dir: string, ext: RegExp = /.settings.json$/) =>
*
*/
export async function findFilesFromMultipleDirectories(...files: string[]): Promise<string[]> {
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

Expand All @@ -90,21 +90,21 @@ export async function findFilesFromMultipleDirectories(...files: string[]): Prom

/*
*/
export const importFiles = (files: string[], results:IStrapiModel[] = [], merge: Partial<IStrapiModel> = {}) =>
export const importFiles = (files: string[], results: IStrapiModel[] = [], merge: Partial<IStrapiModel> = {}) =>
new Promise<IStrapiModel[]>((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);
Expand Down
3 changes: 2 additions & 1 deletion src/test/test1.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/',
Expand Down

0 comments on commit 64d9dcd

Please sign in to comment.