create-rstack 2.0.1 → 2.1.0

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/README.md CHANGED
@@ -17,6 +17,65 @@ A shared package for create-rspack, create-rsbuild, create-rspress and create-rs
17
17
  npm add create-rstack -D
18
18
  ```
19
19
 
20
+ ## Features
21
+
22
+ ### NPM Template Support
23
+
24
+ `create-rstack` supports using npm packages as templates, allowing users to create projects from custom templates published to npm.
25
+
26
+ #### Usage
27
+
28
+ ```bash
29
+ # Using npm package name
30
+ npm create rsbuild@latest my-project -- --template my-template-package
31
+
32
+ # Using scoped package
33
+ npm create rsbuild@latest my-project -- --template @scope/template-package
34
+
35
+ # Using explicit npm: prefix
36
+ npm create rsbuild@latest my-project -- --template npm:my-template-package
37
+
38
+ # With specific version
39
+ npm create rsbuild@latest my-project -- --template my-template-package --template-version 1.2.3
40
+ ```
41
+
42
+ #### Template Package Structure
43
+
44
+ Your npm template package should have one of the following structures:
45
+
46
+ ```
47
+ my-template-package/
48
+ ├── template/ # Preferred
49
+ │ ├── package.json
50
+ │ └── src/
51
+ ├── templates/
52
+ │ └── app/ # Alternative
53
+ └── (root) # Fallback
54
+ ├── package.json
55
+ └── src/
56
+ ```
57
+
58
+ #### Caching Strategy
59
+
60
+ - Templates with `latest` version are always re-installed to ensure the latest version
61
+ - Specific versions are cached in `.temp-templates/` for faster reuse
62
+
63
+ #### API
64
+
65
+ ```typescript
66
+ import {
67
+ isNpmTemplate,
68
+ resolveCustomTemplate,
69
+ resolveNpmTemplate,
70
+ } from 'create-rstack';
71
+
72
+ // Check if template input is an npm package
73
+ if (isNpmTemplate(templateInput)) {
74
+ // Resolve npm template to local path
75
+ const templatePath = resolveCustomTemplate(templateInput, version);
76
+ }
77
+ ```
78
+
20
79
  ## Examples
21
80
 
22
81
  | Project | Link |
package/dist/index.d.ts CHANGED
@@ -11,6 +11,8 @@ export declare type Argv = {
11
11
  skill?: string | string[];
12
12
  packageName?: string;
13
13
  'package-name'?: string;
14
+ templateVersion?: string;
15
+ 'template-version'?: string;
14
16
  };
15
17
 
16
18
  export declare const autocomplete: <Value>(opts: AutocompleteOptions<Value>) => Promise<Value | symbol>;
@@ -182,6 +184,11 @@ declare interface GroupMultiSelectOptions<Value> extends CommonOptions {
182
184
  groupSpacing?: number;
183
185
  }
184
186
 
187
+ /**
188
+ * Check if the input is an npm package template
189
+ */
190
+ export declare function isNpmTemplate(templateInput: string): boolean;
191
+
185
192
  /**
186
193
  * Merge AGENTS.md files from multiple sources
187
194
  */
@@ -257,6 +264,27 @@ declare type Option_2<Value> = Value extends Primitive ? {
257
264
 
258
265
  declare type Primitive = Readonly<string | boolean | number>;
259
266
 
267
+ /**
268
+ * Resolve custom template (npm package, GitHub, or local path)
269
+ */
270
+ export declare function resolveCustomTemplate(templateInput: string, version?: string, options?: {
271
+ forceLatest?: boolean;
272
+ cacheDir?: string;
273
+ }): string;
274
+
275
+ /**
276
+ * Resolve npm template package and return the local path
277
+ */
278
+ export declare function resolveNpmTemplate(packageName: string, version?: string, options?: {
279
+ forceLatest?: boolean;
280
+ cacheDir?: string;
281
+ }): string;
282
+
283
+ /**
284
+ * Sanitize package name and version to create a valid cache key
285
+ */
286
+ export declare const sanitizeCacheKey: (packageName: string, version: string) => string;
287
+
260
288
  export declare const select: <Value>(opts: SelectOptions<Value>) => Promise<Value | symbol>;
261
289
 
262
290
  declare interface SelectOptions<Value> extends CommonOptions {
package/dist/index.js CHANGED
@@ -8,7 +8,7 @@ import node_readline from "node:readline";
8
8
  import node_tty from "node:tty";
9
9
  import node_os from "node:os";
10
10
  import { createRequire } from "node:module";
11
- import { spawn } from "node:child_process";
11
+ import { execFileSync, spawn } from "node:child_process";
12
12
  import { pipeline } from "node:stream/promises";
13
13
  import { PassThrough } from "node:stream";
14
14
  import { createRequire as __rspack_createRequire } from "node:module";
@@ -2940,6 +2940,91 @@ const main_U = (e, t, n)=>{
2940
2940
  return r;
2941
2941
  };
2942
2942
  const main_W = main_U;
2943
+ const NPM_TEMPLATE_PREFIX = 'npm:';
2944
+ const VALID_NPM_NAME = /^(@[\w.-]+\/)?[\w.-]+$/;
2945
+ const VALID_NPM_VERSION = /^[\w.\-+^~>=<* ]+$/;
2946
+ const sanitizeCacheKey = (packageName, version)=>{
2947
+ const normalized = packageName.startsWith('@') ? packageName : packageName.replace(/[\\/]/g, '_');
2948
+ const versionLabel = version || 'latest';
2949
+ return `${normalized}@${versionLabel}`;
2950
+ };
2951
+ function isNpmTemplate(templateInput) {
2952
+ const trimmedInput = templateInput.trim();
2953
+ if (trimmedInput.startsWith(NPM_TEMPLATE_PREFIX)) return true;
2954
+ if (trimmedInput.startsWith('@') || !trimmedInput.includes('/') && !trimmedInput.startsWith('http') && !trimmedInput.startsWith('.') && !trimmedInput.startsWith('github:')) return true;
2955
+ return false;
2956
+ }
2957
+ function resolveNpmTemplate(packageName, version, options) {
2958
+ const normalizedName = packageName.trim();
2959
+ const versionSpecifier = version?.trim() && 'latest' !== version.trim().toLowerCase() ? version.trim() : 'latest';
2960
+ if (!VALID_NPM_NAME.test(normalizedName)) throw new Error(`Invalid npm package name: "${normalizedName}". Package names may only contain word characters, hyphens, and dots.`);
2961
+ if ('latest' !== versionSpecifier && !VALID_NPM_VERSION.test(versionSpecifier)) throw new Error(`Invalid version specifier: "${versionSpecifier}". Version may only contain word characters, dots, hyphens, and range operators.`);
2962
+ const cacheKey = sanitizeCacheKey(normalizedName, versionSpecifier);
2963
+ const cacheRoot = options?.cacheDir || process.cwd();
2964
+ const templateDir = node_path.join(cacheRoot, '.temp-templates', cacheKey);
2965
+ const installRoot = node_path.join(templateDir, '.install');
2966
+ const packagePath = node_path.join(installRoot, 'node_modules', normalizedName);
2967
+ const forceLatest = options?.forceLatest ?? 'latest' === versionSpecifier;
2968
+ const shouldReuseCache = !forceLatest && node_fs.existsSync(templateDir);
2969
+ if (shouldReuseCache) return templateDir;
2970
+ node_fs.mkdirSync(installRoot, {
2971
+ recursive: true
2972
+ });
2973
+ const anchorPkgJson = node_path.join(installRoot, 'package.json');
2974
+ if (!node_fs.existsSync(anchorPkgJson)) {
2975
+ const minimal = {
2976
+ name: 'create-rstack-template-cache',
2977
+ private: true
2978
+ };
2979
+ node_fs.writeFileSync(anchorPkgJson, `${JSON.stringify(minimal, null, 2)}\n`, 'utf8');
2980
+ }
2981
+ try {
2982
+ execFileSync('npm', [
2983
+ 'install',
2984
+ `${normalizedName}@${versionSpecifier}`,
2985
+ '--no-save',
2986
+ '--package-lock=false',
2987
+ '--no-audit',
2988
+ '--no-fund',
2989
+ '--silent'
2990
+ ], {
2991
+ cwd: installRoot,
2992
+ stdio: 'pipe'
2993
+ });
2994
+ } catch (err) {
2995
+ const stderr = err instanceof Error && 'stderr' in err ? String(err.stderr).trim() : '';
2996
+ const detail = stderr ? `\n${stderr.split('\n').slice(0, 5).join('\n')}` : '';
2997
+ throw new Error(`Failed to install npm template "${normalizedName}@${versionSpecifier}". Please check if the package exists.${detail}`);
2998
+ }
2999
+ const possibleTemplatePaths = [
3000
+ node_path.join(packagePath, 'template'),
3001
+ node_path.join(packagePath, 'templates', 'app'),
3002
+ node_path.join(packagePath, 'templates', 'default'),
3003
+ packagePath
3004
+ ];
3005
+ for (const pathCandidate of possibleTemplatePaths)if (node_fs.existsSync(pathCandidate) && node_fs.statSync(pathCandidate).isDirectory()) {
3006
+ if (node_fs.existsSync(templateDir)) {
3007
+ for (const entry of node_fs.readdirSync(templateDir))if ('.install' !== entry) node_fs.rmSync(node_path.join(templateDir, entry), {
3008
+ recursive: true,
3009
+ force: true
3010
+ });
3011
+ }
3012
+ node_fs.cpSync(pathCandidate, templateDir, {
3013
+ recursive: true
3014
+ });
3015
+ return templateDir;
3016
+ }
3017
+ throw new Error(`No valid template directory found in package "${normalizedName}". Expected one of: template/, templates/app/, templates/default/, or package root.`);
3018
+ }
3019
+ function resolveCustomTemplate(templateInput, version, options) {
3020
+ const trimmedInput = templateInput.trim();
3021
+ if (trimmedInput.startsWith(NPM_TEMPLATE_PREFIX)) {
3022
+ const packageName = trimmedInput.slice(NPM_TEMPLATE_PREFIX.length).trim();
3023
+ return resolveNpmTemplate(packageName, version, options);
3024
+ }
3025
+ if (isNpmTemplate(trimmedInput)) return resolveNpmTemplate(trimmedInput, version, options);
3026
+ return trimmedInput;
3027
+ }
2943
3028
  const dist_0 = __webpack_require__("./node_modules/.pnpm/@vercel+detect-agent@1.2.3/node_modules/@vercel/detect-agent/dist/index.js");
2944
3029
  const cjs = __webpack_require__("./node_modules/.pnpm/deepmerge@4.3.1/node_modules/deepmerge/dist/cjs.js");
2945
3030
  var cjs_default = /*#__PURE__*/ __webpack_require__.n(cjs);
@@ -3020,6 +3105,7 @@ function logHelpMessage(name, templates, extraTools, extraSkills) {
3020
3105
  --tools <tool> add additional tools, comma separated
3021
3106
  ${skillsOptionLine} --override override files in target directory
3022
3107
  --packageName <name> specify the package name
3108
+ --template-version <ver> specify the npm template version
3023
3109
 
3024
3110
  Available templates:
3025
3111
  ${templates.join(', ')}
@@ -3123,6 +3209,7 @@ const parseArgv = (processArgv)=>{
3123
3209
  });
3124
3210
  if (!argv.dir && argv._[0]) argv.dir = argv._[0];
3125
3211
  if (argv['package-name']) argv.packageName = argv['package-name'];
3212
+ if (argv['template-version']) argv.templateVersion = argv['template-version'];
3126
3213
  return argv;
3127
3214
  };
3128
3215
  async function runCommand(command, cwd, packageManager) {
@@ -3192,6 +3279,16 @@ async function runSkillCommand(skills, cwd) {
3192
3279
  }
3193
3280
  installationTaskLog.success(`Installed ${skillNoun} ${skillLabel}`);
3194
3281
  }
3282
+ function logNextStepsAndOutro(noteInformation, targetDir, packageManager) {
3283
+ const nextSteps = noteInformation ? noteInformation : [
3284
+ `1. ${color.cyan(`cd ${targetDir}`)}`,
3285
+ `2. ${color.cyan('git init')} ${color.dim('(optional)')}`,
3286
+ `3. ${color.cyan(`${packageManager} install`)}`,
3287
+ `4. ${color.cyan(`${packageManager} run dev`)}`
3288
+ ];
3289
+ if (nextSteps.length) wt(nextSteps.map((step)=>color.reset(step)).join('\n'), 'Next steps');
3290
+ gt('All set, happy coding!');
3291
+ }
3195
3292
  async function create({ name, root, templates, skipFiles, getTemplateName, mapESLintTemplate, version, noteInformation, extraTools, extraSkills, argv: processArgv = process.argv }) {
3196
3293
  src_logger.greet(`\n◆ Create ${upperFirst(name)} Project`);
3197
3294
  const { isAgent } = await (0, dist_0.determineAgent)();
@@ -3237,9 +3334,25 @@ async function create({ name, root, templates, skipFiles, getTemplateName, mapES
3237
3334
  if ('no' === option) cancelAndExit();
3238
3335
  }
3239
3336
  const templateName = await getTemplateName(argv);
3337
+ const srcFolder = node_path.join(root, `template-${templateName}`);
3338
+ if ('string' == typeof argv.template && isNpmTemplate(argv.template) && !node_fs.existsSync(srcFolder)) {
3339
+ const templateVersion = argv.templateVersion ?? argv['template-version'];
3340
+ const templatePath = resolveCustomTemplate(argv.template, templateVersion, {
3341
+ cacheDir: root
3342
+ });
3343
+ copyFolder({
3344
+ from: templatePath,
3345
+ to: distFolder,
3346
+ version,
3347
+ packageName,
3348
+ templateParameters,
3349
+ skipFiles
3350
+ });
3351
+ logNextStepsAndOutro(noteInformation, targetDir, packageManager);
3352
+ return;
3353
+ }
3240
3354
  const tools = await getTools(argv, extraTools, templateName);
3241
3355
  const skills = await getSkills(argv, extraSkills, templateName, tools, yt);
3242
- const srcFolder = node_path.join(root, `template-${templateName}`);
3243
3356
  const commonFolder = node_path.join(root, 'template-common');
3244
3357
  if (!node_fs.existsSync(srcFolder)) throw new Error(`Invalid input: template "${templateName}" not found.`);
3245
3358
  copyFolder({
@@ -3327,14 +3440,7 @@ async function create({ name, root, templates, skipFiles, getTemplateName, mapES
3327
3440
  const agentsPath = node_path.join(distFolder, 'AGENTS.md');
3328
3441
  node_fs.writeFileSync(agentsPath, `${replacePlaceholder(mergedAgents, templateParameters)}\n`);
3329
3442
  }
3330
- const nextSteps = noteInformation ? noteInformation : [
3331
- `1. ${color.cyan(`cd ${targetDir}`)}`,
3332
- `2. ${color.cyan('git init')} ${color.dim('(optional)')}`,
3333
- `3. ${color.cyan(`${packageManager} install`)}`,
3334
- `4. ${color.cyan(`${packageManager} run dev`)}`
3335
- ];
3336
- if (nextSteps.length) wt(nextSteps.map((step)=>color.reset(step)).join('\n'), 'Next steps');
3337
- gt('All set, happy coding!');
3443
+ logNextStepsAndOutro(noteInformation, targetDir, packageManager);
3338
3444
  }
3339
3445
  function sortObjectKeys(obj) {
3340
3446
  const sortedKeys = Object.keys(obj).sort();
@@ -3497,4 +3603,4 @@ function collectAgentsFiles(agentsMdSearchDirs) {
3497
3603
  }
3498
3604
  return agentsFiles;
3499
3605
  }
3500
- export { Ae as autocomplete, BUILTIN_TOOLS, Ot as text, _t as select, checkCancel, collectAgentsFiles, copyFolder, create, ht as groupMultiselect, mergeAgentsFiles, mergePackageJson, yt as multiselect };
3606
+ export { Ae as autocomplete, BUILTIN_TOOLS, Ot as text, _t as select, checkCancel, collectAgentsFiles, copyFolder, create, ht as groupMultiselect, isNpmTemplate, mergeAgentsFiles, mergePackageJson, resolveCustomTemplate, resolveNpmTemplate, sanitizeCacheKey, yt as multiselect };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-rstack",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "description": "Create a new Rstack project",
5
5
  "repository": {
6
6
  "type": "git",
@@ -43,10 +43,10 @@
43
43
  },
44
44
  "devDependencies": {
45
45
  "@clack/prompts": "^1.2.0",
46
- "@microsoft/api-extractor": "^7.58.5",
47
- "@rslib/core": "0.21.2",
48
- "@rslint/core": "^0.4.2",
49
- "@rstest/core": "0.9.8",
46
+ "@microsoft/api-extractor": "^7.58.7",
47
+ "@rslib/core": "0.21.3",
48
+ "@rslint/core": "^0.5.0",
49
+ "@rstest/core": "0.9.9",
50
50
  "@types/fs-extra": "^11.0.4",
51
51
  "@types/minimist": "^1.2.5",
52
52
  "@types/node": "24.12.2",
@@ -64,7 +64,7 @@
64
64
  "engines": {
65
65
  "node": "^20.19.0 || >=22.12.0"
66
66
  },
67
- "packageManager": "pnpm@10.33.0",
67
+ "packageManager": "pnpm@10.33.2",
68
68
  "publishConfig": {
69
69
  "access": "public",
70
70
  "registry": "https://registry.npmjs.org/"
@@ -7,6 +7,6 @@
7
7
  "format": "biome format --write"
8
8
  },
9
9
  "devDependencies": {
10
- "@biomejs/biome": "2.4.12"
10
+ "@biomejs/biome": "2.4.13"
11
11
  }
12
12
  }
@@ -11,6 +11,6 @@
11
11
  "eslint-plugin-react-hooks": "^7.1.1",
12
12
  "eslint-plugin-react-refresh": "^0.5.2",
13
13
  "globals": "^17.5.0",
14
- "typescript-eslint": "^8.58.2"
14
+ "typescript-eslint": "^8.59.0"
15
15
  }
16
16
  }
@@ -8,7 +8,7 @@
8
8
  "devDependencies": {
9
9
  "@eslint/js": "^10.0.1",
10
10
  "eslint": "^10.2.1",
11
- "eslint-plugin-svelte": "^3.17.0",
11
+ "eslint-plugin-svelte": "^3.17.1",
12
12
  "globals": "^17.5.0"
13
13
  }
14
14
  }
@@ -8,9 +8,9 @@
8
8
  "devDependencies": {
9
9
  "@eslint/js": "^10.0.1",
10
10
  "eslint": "^10.2.1",
11
- "eslint-plugin-svelte": "^3.17.0",
11
+ "eslint-plugin-svelte": "^3.17.1",
12
12
  "globals": "^17.5.0",
13
- "typescript-eslint": "^8.58.2"
13
+ "typescript-eslint": "^8.59.0"
14
14
  },
15
15
  "type": "module"
16
16
  }
@@ -9,6 +9,6 @@
9
9
  "@eslint/js": "^10.0.1",
10
10
  "eslint": "^10.2.1",
11
11
  "globals": "^17.5.0",
12
- "typescript-eslint": "^8.58.2"
12
+ "typescript-eslint": "^8.59.0"
13
13
  }
14
14
  }
@@ -8,7 +8,7 @@
8
8
  "devDependencies": {
9
9
  "@eslint/js": "^10.0.1",
10
10
  "eslint": "^10.2.1",
11
- "eslint-plugin-vue": "^10.8.0",
11
+ "eslint-plugin-vue": "^10.9.0",
12
12
  "globals": "^17.5.0"
13
13
  }
14
14
  }
@@ -8,8 +8,8 @@
8
8
  "devDependencies": {
9
9
  "@vue/eslint-config-typescript": "^14.7.0",
10
10
  "eslint": "^10.2.1",
11
- "eslint-plugin-vue": "^10.8.0",
11
+ "eslint-plugin-vue": "^10.9.0",
12
12
  "globals": "^17.5.0",
13
- "typescript-eslint": "^8.58.2"
13
+ "typescript-eslint": "^8.59.0"
14
14
  }
15
15
  }
@@ -6,6 +6,6 @@
6
6
  "lint": "rslint"
7
7
  },
8
8
  "devDependencies": {
9
- "@rslint/core": "^0.4.2"
9
+ "@rslint/core": "^0.5.0"
10
10
  }
11
11
  }