-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: specify the name and template using command line options
- Loading branch information
Showing
34 changed files
with
861 additions
and
3,379 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { readFileSync, writeFileSync } from 'node:fs'; | ||
import path, { resolve } from 'node:path'; | ||
import minimist from 'minimist'; | ||
|
||
export function parseArg() { | ||
const argv = minimist(process.argv.slice(2)); | ||
return { | ||
projectName: argv._[0], | ||
template: argv.t || argv.template | ||
}; | ||
} | ||
export function getPkgFromUserAgent(userAgent: string | undefined) { | ||
if (!userAgent) { | ||
return; | ||
} | ||
|
||
const pkgSpec = userAgent.split(' ')[0]; | ||
const pkgSpecArr = pkgSpec.split('/'); | ||
return { | ||
name: pkgSpecArr[0], | ||
version: pkgSpecArr[1], | ||
}; | ||
} | ||
|
||
export function getPkgInfo() { | ||
const pkgInfo = getPkgFromUserAgent(process.env.npm_config_user_agent); | ||
return pkgInfo; | ||
} | ||
|
||
export function getPkgManager() { | ||
const pkgInfo = getPkgInfo(); | ||
return pkgInfo ? pkgInfo.name : 'npm'; | ||
} | ||
|
||
export function printActionsInfo(targetDir: string) { | ||
const pkgManager = getPkgManager(); | ||
console.log(); | ||
console.log(` cd ${targetDir}`); | ||
switch (pkgManager) { | ||
case 'yarn': | ||
console.log(' yarn'); | ||
console.log(' yarn dev'); | ||
break; | ||
default: | ||
console.log(` ${pkgManager} install`); | ||
console.log(` ${pkgManager} run dev`); | ||
break; | ||
} | ||
console.log(); | ||
} | ||
|
||
export function toValidPackageName(projectName: string) { | ||
return projectName | ||
.trim() | ||
.toLowerCase() | ||
.replace(/\s+/g, '-') | ||
.replace(/^[._]/, '') | ||
.replace(/[^a-z\d\-~]+/g, '-'); | ||
} | ||
|
||
export function renamePackageName(projectName: string, targetPath: string) { | ||
if (!projectName) | ||
return; | ||
|
||
const pkg = JSON.parse( | ||
readFileSync(resolve(targetPath, 'package.json'), 'utf-8') | ||
); | ||
|
||
pkg.name = toValidPackageName(projectName); | ||
|
||
writeFileSync(path.resolve(targetPath, 'package.json'), `${JSON.stringify(pkg, null, 2)}\n`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,42 @@ | ||
{ | ||
"title": "project type", | ||
"message": "请选择项目类型", | ||
"message": "Please select the project type:", | ||
"description": "Select project type", | ||
"type": "select", | ||
"items": { | ||
"cli": { | ||
"title": "cli", | ||
"message": "", | ||
"title": "CLI", | ||
"message": "Please select CLI project template:", | ||
"description": "CLI project template", | ||
"type": "select", | ||
"items": { | ||
"unbuild": { | ||
"title": "cli-unbuild", | ||
"message": "", | ||
"description": "Use Unbuild as the packaging and building tool CLI template.", | ||
"repo": "hacxy/cli-unbuild-template" | ||
}, | ||
"tsup": { | ||
"title": "cli-tsup", | ||
"message": "", | ||
"description": "Use Tsup as the packaging and building tool CLI template.", | ||
"repo": "hacxy/cli-tsup-template" | ||
} | ||
} | ||
}, | ||
"library": { | ||
"title": "library", | ||
"message": "", | ||
"message": "Please select a library project template:", | ||
"description": "Library project template", | ||
"type": "select", | ||
"items": { | ||
"vite": { | ||
"title": "library-vite", | ||
"message": "", | ||
"repo": "hacxy/library-empty-template" | ||
"description": "Use Vite as the packaging and building tool library template.", | ||
"repo": "hacxy/library-vite-template" | ||
} | ||
} | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.