Skip to content

Commit 61f34f3

Browse files
committed
🔨 Updates config init script
1 parent 7de8478 commit 61f34f3

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

packages/initializer/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"dependencies": {
1313
"fs-extra": "^9.1.0",
1414
"semver": "^7.3.5",
15+
"recast": "^0.20.4",
1516
"ts-node": "^9.1.1"
1617
}
1718
}

packages/initializer/src/index.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from 'fs-extra';
22
import semver from 'semver';
3+
import * as recast from 'recast';
34

45
function transformPackageName(packageName: string) {
56
return packageName.replace('/', '__');
@@ -38,10 +39,51 @@ function main(packageName: string, version: string) {
3839
fs.writeFileSync(
3940
configPath,
4041
`export default {
41-
maintainers: ['danieldelcore'],
42+
maintainers: [],
43+
transforms: {
44+
'${version}': require('./${version}/transform'),
45+
}
4246
};
4347
`,
4448
);
49+
} else {
50+
const source = fs.readFileSync(configPath, 'utf8');
51+
const ast = recast.parse(source);
52+
const b = recast.types.builders;
53+
54+
recast.visit(ast, {
55+
visitProperty(path) {
56+
// @ts-ignore
57+
if (path.node.key.name !== 'transforms') return false;
58+
// @ts-ignore
59+
const properties = path.node.value.properties;
60+
// @ts-ignore
61+
properties.forEach(property => {
62+
if (semver.eq(property.key.value, version)) {
63+
throw new Error(
64+
`Transform for ${packageName} version ${version} already exists`,
65+
);
66+
}
67+
});
68+
69+
properties.push(
70+
b.property(
71+
'init',
72+
b.stringLiteral(version),
73+
b.callExpression(b.identifier('require'), [
74+
b.stringLiteral(`./${version}/transform`),
75+
]),
76+
),
77+
);
78+
79+
return false;
80+
},
81+
});
82+
83+
fs.writeFileSync(
84+
configPath,
85+
recast.prettyPrint(ast, { quote: 'single', trailingComma: true }).code,
86+
);
4587
}
4688

4789
fs.copyFileSync(

0 commit comments

Comments
 (0)