create-hsi-app 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/create-hsi-app.mjs +130 -0
- package/package.json +16 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { execFileSync } from 'node:child_process';
|
|
3
|
+
import {
|
|
4
|
+
existsSync,
|
|
5
|
+
readdirSync,
|
|
6
|
+
readFileSync,
|
|
7
|
+
rmSync,
|
|
8
|
+
writeFileSync,
|
|
9
|
+
} from 'node:fs';
|
|
10
|
+
import { basename, join, resolve } from 'node:path';
|
|
11
|
+
|
|
12
|
+
const templateRepo = 'https://github.com/Hsiii/frontend-template.git';
|
|
13
|
+
const defaultAppName = 'my-hsi-app';
|
|
14
|
+
const targetArg = process.argv[2] ?? defaultAppName;
|
|
15
|
+
const targetPath = resolve(targetArg);
|
|
16
|
+
const appName = toPackageName(basename(targetPath));
|
|
17
|
+
|
|
18
|
+
if (existsSync(targetPath) && readdirSync(targetPath).length > 0) {
|
|
19
|
+
fail(`Target directory is not empty: ${targetPath}`);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
run('git', ['clone', '--depth', '1', templateRepo, targetPath]);
|
|
23
|
+
|
|
24
|
+
rmSync(join(targetPath, '.git'), { force: true, recursive: true });
|
|
25
|
+
rmSync(join(targetPath, '.github'), { force: true, recursive: true });
|
|
26
|
+
rmSync(join(targetPath, 'packages'), { force: true, recursive: true });
|
|
27
|
+
|
|
28
|
+
updatePackageJson();
|
|
29
|
+
updateBunLock();
|
|
30
|
+
updateAppText();
|
|
31
|
+
writeAppReadme();
|
|
32
|
+
|
|
33
|
+
console.log(`\nCreated ${appName} in ${targetPath}\n`);
|
|
34
|
+
console.log('Next steps:');
|
|
35
|
+
console.log(` cd ${targetArg}`);
|
|
36
|
+
console.log(' bun i');
|
|
37
|
+
console.log(' bun run dev');
|
|
38
|
+
|
|
39
|
+
function run(command, args) {
|
|
40
|
+
try {
|
|
41
|
+
execFileSync(command, args, { stdio: 'inherit' });
|
|
42
|
+
} catch {
|
|
43
|
+
fail(`Failed to run: ${command} ${args.join(' ')}`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function updatePackageJson() {
|
|
48
|
+
const packageJsonPath = join(targetPath, 'package.json');
|
|
49
|
+
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
|
|
50
|
+
|
|
51
|
+
packageJson.name = appName;
|
|
52
|
+
packageJson.version = '0.1.0';
|
|
53
|
+
delete packageJson.repository;
|
|
54
|
+
delete packageJson.publishConfig;
|
|
55
|
+
|
|
56
|
+
writeFileSync(packageJsonPath, `${JSON.stringify(packageJson, null, 4)}\n`);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function updateBunLock() {
|
|
60
|
+
const lockPath = join(targetPath, 'bun.lock');
|
|
61
|
+
|
|
62
|
+
if (!existsSync(lockPath)) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const lock = readFileSync(lockPath, 'utf8').replace(
|
|
67
|
+
'"name": "@hsiii/hsi-app"',
|
|
68
|
+
`"name": "${appName}"`
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
writeFileSync(lockPath, lock);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function updateAppText() {
|
|
75
|
+
replaceInFile(join(targetPath, 'index.html'), '<title>hsi-app</title>', {
|
|
76
|
+
with: `<title>${appName}</title>`,
|
|
77
|
+
});
|
|
78
|
+
replaceInFile(join(targetPath, 'src/components/App.tsx'), '>hsi-app<', {
|
|
79
|
+
with: `>${appName}<`,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function writeAppReadme() {
|
|
84
|
+
const readme = `# ${appName}
|
|
85
|
+
|
|
86
|
+
Created from the hsi-app frontend template.
|
|
87
|
+
|
|
88
|
+
## Install
|
|
89
|
+
|
|
90
|
+
\`\`\`bash
|
|
91
|
+
bun i
|
|
92
|
+
\`\`\`
|
|
93
|
+
|
|
94
|
+
## Develop
|
|
95
|
+
|
|
96
|
+
\`\`\`bash
|
|
97
|
+
bun run dev
|
|
98
|
+
\`\`\`
|
|
99
|
+
|
|
100
|
+
## Check
|
|
101
|
+
|
|
102
|
+
\`\`\`bash
|
|
103
|
+
bun run check
|
|
104
|
+
\`\`\`
|
|
105
|
+
`;
|
|
106
|
+
|
|
107
|
+
writeFileSync(join(targetPath, 'README.md'), readme);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function replaceInFile(filePath, searchValue, replacement) {
|
|
111
|
+
const source = readFileSync(filePath, 'utf8');
|
|
112
|
+
writeFileSync(filePath, source.replace(searchValue, replacement.with));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function toPackageName(value) {
|
|
116
|
+
const name = value
|
|
117
|
+
.trim()
|
|
118
|
+
.toLowerCase()
|
|
119
|
+
.replaceAll(/[\s_]+/g, '-')
|
|
120
|
+
.replaceAll(/[^a-z0-9-.]/g, '')
|
|
121
|
+
.replaceAll(/^[.-]+|[.-]+$/g, '')
|
|
122
|
+
.replaceAll(/-{2,}/g, '-');
|
|
123
|
+
|
|
124
|
+
return name || defaultAppName;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function fail(message) {
|
|
128
|
+
console.error(`create-hsi-app: ${message}`);
|
|
129
|
+
process.exit(1);
|
|
130
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "create-hsi-app",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Create a new app from the hsi-app frontend template.",
|
|
6
|
+
"bin": {
|
|
7
|
+
"create-hsi-app": "bin/create-hsi-app.mjs"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"bin"
|
|
11
|
+
],
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">=18"
|
|
14
|
+
},
|
|
15
|
+
"license": "UNLICENSED"
|
|
16
|
+
}
|