-
-
Notifications
You must be signed in to change notification settings - Fork 120
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
lukasz.klejszta
committed
Jun 29, 2022
1 parent
1327dea
commit fd7c463
Showing
3 changed files
with
46 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const fs = require('fs'); | ||
|
||
const [libDirectoryPath] = process.argv.slice(2); | ||
const GENERATE_INDICATOR = "@automaticallyGeneratedExamples" | ||
// https://regex101.com/r/rlqGTA/1 | ||
const regexp = new RegExp(`(${GENERATE_INDICATOR}).*\\/`,"gs"); | ||
const errors = []; | ||
|
||
console.log('Generating examples...') | ||
const filenames = fs | ||
.readdirSync(libDirectoryPath) | ||
.filter((jsonFilename, index, array) => jsonFilename.includes(".json") && array.find(tsFilename => tsFilename.replace('ts','json') === jsonFilename)) | ||
|
||
filenames.forEach(filename => { | ||
try { | ||
const tsFilepath = `${libDirectoryPath}${filename.replace('json','ts')}` | ||
const tsFileContent = fs.readFileSync(tsFilepath).toString(); | ||
if(!tsFileContent.includes(GENERATE_INDICATOR)) { | ||
console.log(`file is missing '${GENERATE_INDICATOR}' and will be ignored ${filename.replace('json','ts')}`); | ||
return; | ||
} | ||
const jsonFilepath = `${libDirectoryPath}${filename}` | ||
const jsonFileContent = JSON.parse(fs.readFileSync(jsonFilepath)); | ||
if(jsonFileContent.data === undefined) { | ||
throw Error(`missing data field in file: ${filename}`); | ||
} | ||
if(!(jsonFileContent.data instanceof Array)) { | ||
throw Error(`invalid data field, must be an array: ${filename}`); | ||
} | ||
fs.writeFileSync(tsFilepath, tsFileContent.replace(regexp, `${GENERATE_INDICATOR}\n * @example\n${jsonFileContent.data.slice(0,3).map(example => ` * ${example}\n`).join('')} */`)); | ||
} catch(error) { | ||
errors.push({filename, error}); | ||
} | ||
}); | ||
|
||
if(errors.length > 0) { | ||
console.log(errors); | ||
} | ||
console.log(`Generating examples finished ${filenames.length - errors.length}/${filenames.length}`); |