Skip to content

Commit d35e315

Browse files
committed
fix(cli-forge): fix bin entries for init to exclude .ts extension
1 parent fbdfe7d commit d35e315

File tree

1 file changed

+41
-3
lines changed
  • packages/cli-forge/bin/commands

1 file changed

+41
-3
lines changed

packages/cli-forge/bin/commands/init.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ export const initCommand = cli('init', {
6666
args.output ??= join(process.cwd(), args.cliName);
6767
ensureDirSync(args.output);
6868
const packageJsonPath = join(args.output, 'package.json');
69-
const cliPath = join(args.output, 'bin', `${args.cliName}.${args.format}`);
69+
const cliPathWithoutExtension = join(args.output, 'bin', `${args.cliName}`);
70+
const cliPath = [cliPathWithoutExtension, args.format].join('.');
7071

7172
let packageJsonContent: PackageJson = readJsonOr(packageJsonPath, {
7273
name: args.cliName,
@@ -76,7 +77,7 @@ export const initCommand = cli('init', {
7677
name: args.cliName,
7778
version: args.initialVersion,
7879
bin: {
79-
[args.cliName]: relative(args.output, cliPath),
80+
[args.cliName]: relative(args.output, cliPathWithoutExtension),
8081
},
8182
dependencies: {
8283
'cli-forge': CLI_FORGE_VERSION,
@@ -131,7 +132,21 @@ cpSync('package.json', 'dist/package.json');
131132
)
132133
);
133134
}
134-
writeFileSync(packageJsonPath, JSON.stringify(packageJsonContent, null, 2));
135+
writeFileSync(
136+
packageJsonPath,
137+
JSON.stringify(
138+
orderKeysInJson(packageJsonContent, [
139+
'name',
140+
'version',
141+
'scripts',
142+
'bin',
143+
'dependencies',
144+
'devDependencies',
145+
]),
146+
null,
147+
2
148+
)
149+
);
135150
ensureDirSync(dirname(cliPath));
136151
writeFileSync(
137152
cliPath,
@@ -273,3 +288,26 @@ function mergePackageJsonContents(
273288

274289
return merged;
275290
}
291+
292+
function orderKeysInJson<T extends Record<string, unknown>>(
293+
obj: T,
294+
order: Array<keyof T & string>
295+
): T {
296+
const values = new Map(Object.entries(obj));
297+
const keys = new Set(Object.keys(obj));
298+
const returnObj = {} as T;
299+
300+
for (const key of order) {
301+
const value = values.get(key);
302+
if (value !== undefined) {
303+
returnObj[key] = value as T[typeof key];
304+
keys.delete(key);
305+
}
306+
}
307+
308+
for (const key of keys) {
309+
(returnObj as any)[key] = values.get(key) as T[typeof key];
310+
}
311+
312+
return returnObj;
313+
}

0 commit comments

Comments
 (0)