Skip to content

Commit c2cbeab

Browse files
author
Joshua Lawrence
committed
Updated update-docs command to respect mixed file type extensions
- This is needed when working on transforms that require different i/o file types - The regex is used to only process files with 'input.' or 'output.' in their filename. For some reason lookbehind groups are deemed invalid by eslint, which should be supported in these v ersions of node, but I wasn't able to get eslint to accept it. It would provide an additional layer of accuracy by ensuring a '.input.' pattern, but I feel confident that the lookahead is safe to use given the relatively structured nature of fixture filenames.
1 parent 8713765 commit c2cbeab

File tree

2 files changed

+74
-27
lines changed

2 files changed

+74
-27
lines changed

commands/local/update-docs.js

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,46 @@ function updateTransformREADME(transformName) {
4040
return;
4141
}
4242

43-
fs.readdirSync(fixtureDir)
44-
.filter(filename => /\.input$/.test(path.basename(filename, path.extname(filename))))
45-
.forEach(filename => {
46-
let extension = path.extname(filename);
47-
let testName = filename.replace(`.input${extension}`, '');
48-
let inputPath = path.join(fixtureDir, `${testName}.input${extension}`);
49-
let outputPath = path.join(fixtureDir, `${testName}.output${extension}`);
50-
51-
toc.push(`* [${testName}](#${testName})`);
52-
details.push(
53-
'---',
54-
`<a id="${testName}">**${testName}**</a>`,
55-
'',
56-
`**Input** (<small>[${testName}.input${extension}](${inputPath})</small>):`,
57-
'```' + extension.slice(1),
58-
fs.readFileSync(inputPath),
59-
'```',
60-
'',
61-
`**Output** (<small>[${testName}.output${extension}](${outputPath})</small>):`,
62-
'```' + extension.slice(1),
63-
fs.readFileSync(outputPath),
64-
'```'
65-
);
66-
});
43+
const testMap = fs.readdirSync(fixtureDir).reduce((_hash, filename) => {
44+
const extension = path.extname(filename);
45+
const validFilename = path.basename(filename).match(/input|output+(?=\.)/);
46+
if (!validFilename) {
47+
return _hash;
48+
}
49+
const testName = filename.slice(0, validFilename.index - 1);
50+
const testType = validFilename[0];
51+
52+
if (!_hash[testName]) {
53+
_hash[testName] = {};
54+
}
55+
56+
_hash[testName][testType] = `${testName}.${testType}${extension}`;
57+
58+
return _hash;
59+
}, {});
60+
61+
Object.entries(testMap).forEach(([testName, testPaths]) => {
62+
let inputExtension = path.extname(testPaths.input);
63+
let outputExtension = path.extname(testPaths.output);
64+
let inputPath = path.join(fixtureDir, testPaths.input);
65+
let outputPath = path.join(fixtureDir, testPaths.output);
66+
67+
toc.push(`* [${testName}](#${testName})`);
68+
details.push(
69+
'---',
70+
`<a id="${testName}">**${testName}**</a>`,
71+
'',
72+
`**Input** (<small>[${testPaths.input}](${inputPath})</small>):`,
73+
'```' + inputExtension.slice(1),
74+
fs.readFileSync(inputPath),
75+
'```',
76+
'',
77+
`**Output** (<small>[${testPaths.output}](${outputPath})</small>):`,
78+
'```' + outputExtension.slice(1),
79+
fs.readFileSync(outputPath),
80+
'```'
81+
);
82+
});
6783

6884
let transformREADMEPath = `transforms/${transformName}/README.md`;
6985

tests/cli-test.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,37 @@ QUnit.module('codemod-cli', function(hooks) {
105105
assert.ok(README.includes('* [foo](transforms/foo/README.md'), 'foo link');
106106
assert.ok(README.includes('* [bar](transforms/bar/README.md'), 'bar link');
107107
});
108+
109+
QUnit.test('should allow different input/output extensions', async function(assert) {
110+
codemodProject.write({
111+
transforms: {
112+
main: {
113+
__testfixtures__: {
114+
'basic.input.js': '"starting content";',
115+
'basic.output.hbs': '"different content";',
116+
},
117+
'README.md': `
118+
<!--FIXTURES_TOC_START-->
119+
<!--FIXTURES_TOC_END-->
120+
<!--FIXTURES_CONTENT_START-->
121+
<!--FIXTURES_CONTENT_END-->
122+
`,
123+
},
124+
},
125+
});
126+
127+
await execa(EXECUTABLE_PATH, ['update-docs']);
128+
129+
let README = fs.readFileSync(codemodProject.path('transforms/main/README.md'), 'utf8');
130+
assert.ok(
131+
README.includes('[basic.input.js](transforms/main/__testfixtures__/basic.input.js)'),
132+
'input link'
133+
);
134+
assert.ok(
135+
README.includes('[basic.output.hbs](transforms/main/__testfixtures__/basic.output.hbs)'),
136+
'output link'
137+
);
138+
});
108139
});
109140

110141
QUnit.module('generate', function(hooks) {
@@ -687,18 +718,18 @@ QUnit.module('codemod-cli', function(hooks) {
687718
module.exports = function ({ source /*, path*/ }, { parse, visit }) {
688719
const ast = parse(source);
689720
const options = getOptions();
690-
721+
691722
return visit(ast, (env) => {
692723
let { builders: b } = env.syntax;
693-
724+
694725
return {
695726
MustacheStatement() {
696727
return b.mustache(b.path(options.biz + options.baz));
697728
},
698729
};
699730
});
700731
};
701-
732+
702733
module.exports.type = 'hbs';
703734
`,
704735
},

0 commit comments

Comments
 (0)