2022-01-25 11:34:16 +01:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
2021-07-06 19:22:12 -05:00
|
|
|
|
|
|
|
// Generates an array with the output of processing filenames with an expected
|
2021-10-21 10:07:52 -07:00
|
|
|
// format (`step-###.md`).
|
|
|
|
// ['step-001.md', 'step-002.md'] => [1, 2]
|
2022-01-25 11:34:16 +01:00
|
|
|
function getExistingStepNums(projectPath: string): number[] {
|
2021-07-06 19:22:12 -05:00
|
|
|
return fs.readdirSync(projectPath).reduce((stepNums, fileName) => {
|
|
|
|
if (
|
|
|
|
path.extname(fileName).toLowerCase() === '.md' &&
|
|
|
|
!fileName.endsWith('final.md')
|
|
|
|
) {
|
2022-01-25 11:34:16 +01:00
|
|
|
const stepNumString = fileName.split('.')[0].split('-')[1];
|
2021-07-06 19:22:12 -05:00
|
|
|
|
2022-01-25 11:34:16 +01:00
|
|
|
if (!/^\d{3}$/.test(stepNumString)) {
|
2021-07-06 19:22:12 -05:00
|
|
|
throw (
|
|
|
|
`Step not created. File ${fileName} has a step number containing non-digits.` +
|
|
|
|
' Please run reorder-steps script first.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-25 11:34:16 +01:00
|
|
|
const stepNum = parseInt(stepNumString, 10);
|
2021-07-06 19:22:12 -05:00
|
|
|
stepNums.push(stepNum);
|
|
|
|
}
|
|
|
|
|
|
|
|
return stepNums;
|
2022-01-25 11:34:16 +01:00
|
|
|
}, [] as number[]);
|
2021-07-06 19:22:12 -05:00
|
|
|
}
|
|
|
|
|
2022-01-25 11:34:16 +01:00
|
|
|
export { getExistingStepNums };
|