@project-selene/create-mod 0.0.5 → 0.0.6
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/dist/main.mjs +1 -1
- package/package.json +1 -1
- package/src/main.mts +1 -1
- package/bin/init.mjs +0 -136
package/dist/main.mjs
CHANGED
|
@@ -5,7 +5,7 @@ import { promises as fsp } from "fs";
|
|
|
5
5
|
import inquirer, {} from "inquirer";
|
|
6
6
|
import path from "path";
|
|
7
7
|
import { v4 as uuidv4 } from "uuid";
|
|
8
|
-
const __templateDir = path.resolve(import.meta.url, "
|
|
8
|
+
const __templateDir = path.resolve(import.meta.url.substring("file://".length), "../../template");
|
|
9
9
|
const descriptions = {
|
|
10
10
|
name: "The name displayed to mod users",
|
|
11
11
|
packageName: "The name used for the npm package (no spaces, lowercase)",
|
package/package.json
CHANGED
package/src/main.mts
CHANGED
|
@@ -7,7 +7,7 @@ import inquirer, { type DistinctQuestion } from 'inquirer';
|
|
|
7
7
|
import path from 'path';
|
|
8
8
|
import { v4 as uuidv4 } from "uuid";
|
|
9
9
|
|
|
10
|
-
const __templateDir = path.resolve(import.meta.url, '
|
|
10
|
+
const __templateDir = path.resolve(import.meta.url.substring('file://'.length), '../../template');
|
|
11
11
|
|
|
12
12
|
interface Arguments {
|
|
13
13
|
packageName?: string;
|
package/bin/init.mjs
DELETED
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import fs from 'fs';
|
|
3
|
-
import { promises as fsp } from 'fs';
|
|
4
|
-
import path from 'path';
|
|
5
|
-
import readline from 'readline';
|
|
6
|
-
import { spawn } from 'child_process';
|
|
7
|
-
|
|
8
|
-
function question(prompt) {
|
|
9
|
-
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
10
|
-
return new Promise((resolve) => rl.question(prompt, (ans) => { rl.close(); resolve(ans); }));
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function parseArgs(argv) {
|
|
14
|
-
const res = { _: [] };
|
|
15
|
-
for (let i = 0; i < argv.length; i++) {
|
|
16
|
-
const a = argv[i];
|
|
17
|
-
if (a.startsWith('--')) {
|
|
18
|
-
const eq = a.indexOf('=');
|
|
19
|
-
if (eq !== -1) {
|
|
20
|
-
res[a.slice(2, eq)] = a.slice(eq + 1);
|
|
21
|
-
} else {
|
|
22
|
-
const key = a.slice(2);
|
|
23
|
-
const next = argv[i + 1];
|
|
24
|
-
if (next && !next.startsWith('--')) { res[key] = next; i++; } else { res[key] = true; }
|
|
25
|
-
}
|
|
26
|
-
} else {
|
|
27
|
-
res._.push(a);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
return res;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
async function copyDir(src, dest) {
|
|
34
|
-
await fsp.mkdir(dest, { recursive: true });
|
|
35
|
-
const entries = await fsp.readdir(src, { withFileTypes: true });
|
|
36
|
-
for (const ent of entries) {
|
|
37
|
-
const srcPath = path.join(src, ent.name);
|
|
38
|
-
const destPath = path.join(dest, ent.name);
|
|
39
|
-
if (ent.isDirectory()) {
|
|
40
|
-
await copyDir(srcPath, destPath);
|
|
41
|
-
} else if (ent.isSymbolicLink()) {
|
|
42
|
-
const link = await fsp.readlink(srcPath);
|
|
43
|
-
await fsp.symlink(link, destPath);
|
|
44
|
-
} else {
|
|
45
|
-
await fsp.copyFile(srcPath, destPath);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
async function runNpmInstall(cwd) {
|
|
51
|
-
return new Promise((resolve, reject) => {
|
|
52
|
-
const cmd = process.platform === 'win32' ? 'npm.cmd' : 'npm';
|
|
53
|
-
const child = spawn(cmd, ['install'], { cwd, stdio: 'inherit' });
|
|
54
|
-
child.on('close', (code) => code === 0 ? resolve() : reject(new Error('npm install failed')));
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
async function main() {
|
|
59
|
-
const args = parseArgs(process.argv.slice(2));
|
|
60
|
-
let destName = args._[0] || args.dir || args.name;
|
|
61
|
-
if (!destName) {
|
|
62
|
-
destName = (await question('Project folder name: ')).trim();
|
|
63
|
-
if (!destName) { console.error('No folder name provided. Exiting.'); process.exit(1); }
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const cwd = process.cwd();
|
|
67
|
-
const templatePath = path.join(cwd, 'template');
|
|
68
|
-
const destPath = path.join(cwd, destName);
|
|
69
|
-
|
|
70
|
-
try {
|
|
71
|
-
const stat = await fsp.stat(templatePath);
|
|
72
|
-
if (!stat.isDirectory()) throw new Error('template is not a directory');
|
|
73
|
-
} catch (err) {
|
|
74
|
-
console.error('Could not find template directory at', templatePath);
|
|
75
|
-
process.exit(1);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
try {
|
|
79
|
-
const exists = await fsp.stat(destPath).then(() => true).catch(() => false);
|
|
80
|
-
if (exists) {
|
|
81
|
-
const ans = await question(`Folder ${destName} already exists. Overwrite? (y/N) `);
|
|
82
|
-
if (!/^y(es)?$/i.test(ans.trim())) { console.log('Aborted.'); process.exit(0); }
|
|
83
|
-
}
|
|
84
|
-
await copyDir(templatePath, destPath);
|
|
85
|
-
console.log('Template copied to', destPath);
|
|
86
|
-
|
|
87
|
-
const pkgPath = path.join(destPath, 'package.json');
|
|
88
|
-
const pkgExists = await fsp.stat(pkgPath).then(() => true).catch(() => false);
|
|
89
|
-
if (pkgExists) {
|
|
90
|
-
const raw = await fsp.readFile(pkgPath, 'utf8');
|
|
91
|
-
let pkg;
|
|
92
|
-
try { pkg = JSON.parse(raw); } catch (e) { pkg = null; }
|
|
93
|
-
|
|
94
|
-
const provided = {
|
|
95
|
-
name: args.name || args._[0],
|
|
96
|
-
description: args.description || args.desc || args.d,
|
|
97
|
-
author: args.author || args.a,
|
|
98
|
-
version: args.version || args.v,
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
for (const key of ['name', 'description', 'author', 'version']) {
|
|
102
|
-
if (!provided[key]) {
|
|
103
|
-
const current = pkg && pkg[key] ? pkg[key] : '';
|
|
104
|
-
const ans = await question(`${key} (${current}): `);
|
|
105
|
-
provided[key] = ans.trim() || current || undefined;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
if (pkg) {
|
|
110
|
-
if (provided.name) pkg.name = provided.name;
|
|
111
|
-
if (provided.description) pkg.description = provided.description;
|
|
112
|
-
if (provided.author) pkg.author = provided.author;
|
|
113
|
-
if (provided.version) pkg.version = provided.version;
|
|
114
|
-
await fsp.writeFile(pkgPath, JSON.stringify(pkg, null, 2) + '\n', 'utf8');
|
|
115
|
-
console.log('Updated package.json with provided values.');
|
|
116
|
-
} else {
|
|
117
|
-
// fallback simple token replace
|
|
118
|
-
let replaced = raw.replace(/{{\s*name\s*}}/g, provided.name || '')
|
|
119
|
-
.replace(/{{\s*description\s*}}/g, provided.description || '')
|
|
120
|
-
.replace(/{{\s*author\s*}}/g, provided.author || '')
|
|
121
|
-
.replace(/{{\s*version\s*}}/g, provided.version || '');
|
|
122
|
-
await fsp.writeFile(pkgPath, replaced, 'utf8');
|
|
123
|
-
console.log('Replaced tokens in package.json.');
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
console.log('Installing dependencies...');
|
|
128
|
-
await runNpmInstall(destPath);
|
|
129
|
-
console.log('Done. Project initialized at', destPath);
|
|
130
|
-
} catch (err) {
|
|
131
|
-
console.error('Error:', err.message || err);
|
|
132
|
-
process.exit(1);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
main();
|