diff --git a/src/index.ts b/src/index.ts index f8a31d9..fa6a616 100644 --- a/src/index.ts +++ b/src/index.ts @@ -119,20 +119,9 @@ async function init() { spinner.succeed() - const pkg = pkgRead(root) - pkg.name = targetDir - switch (framework) { - case 'cli-starter': - pkg.bin = { - [targetDir]: './bin/index.mjs', - } - break - case 'vue-component-starter': - break - case 'ts-starter': - break - } - pkgWrite(root, pkg) + const dirName = targetDir.split('/').pop() || '' + + replacePkgName(root, dirName) console.log('\nDone. Now run:\n') console.log(` cd ${targetDir}`) @@ -160,12 +149,18 @@ function emptyDir(dir: string) { } } -function pkgRead(path: string) { - const pkg = JSON.parse( - fs.readFileSync(resolve(path, 'package.json'), 'utf-8'), - ) - return pkg -} -function pkgWrite(root: string, pkg: any) { - fs.writeFileSync(resolve(root, 'package.json'), JSON.stringify(pkg, null, 2)) +function replacePkgName(root: string, target: string) { + // foreach root subfile + const files = fs.readdirSync(root) + for (const file of files) { + const filePath = resolve(root, file) + + if (fs.statSync(filePath).isDirectory()) { + replacePkgName(filePath, target) + } + else { + const content = fs.readFileSync(filePath, 'utf8') + fs.writeFileSync(filePath, content.replaceAll('__pkg_name_placeholder__', target)) + } + } } diff --git a/templates/cli-starter/package.json b/templates/cli-starter/package.json index 98e8446..967a473 100644 --- a/templates/cli-starter/package.json +++ b/templates/cli-starter/package.json @@ -1,5 +1,5 @@ { - "name": "cli-starter", + "name": "__pkg_name_placeholder__", "version": "1.0.0", "packageManager": "pnpm@8.10.2", "description": "A command line tool template", @@ -15,12 +15,12 @@ "typescript", "template" ], - "homepage": "https://github.com/peterroe/cli-starter#readme", + "homepage": "https://github.com/peterroe/__pkg_name_placeholder__#readme", "repository": { "type": "git", - "url": "git+https://github.com/peterroe/cli-starter.git" + "url": "git+https://github.com/peterroe/__pkg_name_placeholder__.git" }, - "bugs": "https://github.com/peterroe/cli-starter/issues", + "bugs": "https://github.com/peterroe/__pkg_name_placeholder__/issues", "main": "./dist/index.mjs", "module": "./dist/index.mjs", "types": "./dist/index.d.ts", @@ -33,7 +33,7 @@ "./*": "./*" }, "bin": { - "cli-starter": "./bin/index.mjs" + "__pkg_name_placeholder__": "./bin/index.mjs" }, "scripts": { "build": "unbuild", diff --git a/templates/docs-starter/.vitepress/config.ts b/templates/docs-starter/.vitepress/config.ts index f8663ea..c891a00 100644 --- a/templates/docs-starter/.vitepress/config.ts +++ b/templates/docs-starter/.vitepress/config.ts @@ -1,6 +1,5 @@ import { defineConfig } from 'vitepress' import type { DefaultTheme } from 'vitepress/types' -import pkg from '../package.json' const title = 'Front End' const description = 'The docs template for the front end' @@ -35,10 +34,10 @@ export default defineConfig({ titleTemplate: title, description, outDir: './dist', - base: `/${pkg.name}/`, + base: '/__pkg_name_placeholder__/', head: [ - ['link', { rel: 'icon', href: '/favicon.svg', type: 'image/svg+xml' }], - ['link', { rel: 'alternate icon', href: '/favicon.ico', type: 'image/png', sizes: '16x16' }], + ['link', { rel: 'icon', href: '/__pkg_name_placeholder__/favicon.svg', type: 'image/svg+xml' }], + ['link', { rel: 'alternate icon', href: '/__pkg_name_placeholder__/favicon.ico', type: 'image/png', sizes: '16x16' }], ['meta', { property: 'og:type', content: 'website' }], ['meta', { name: 'og:title', content: title }], ['meta', { name: 'og:description', content: description }], @@ -70,11 +69,11 @@ export default defineConfig({ '/guide/': SidebarGuide, }, editLink: { - pattern: 'https://github.com/peterroe/un/edit/main/templates/docs-starter/:paht', + pattern: 'https://github.com/peterroe/__pkg_name_placeholder__/edit/main/:paht', text: 'Suggest changes to this page', }, socialLinks: [ - { icon: 'github', link: 'https://github.com/peterroe/un' }, + { icon: 'github', link: 'https://github.com/peterroe/__pkg_name_placeholder__' }, ], footer: { message: 'Released under the MIT License.', diff --git a/templates/docs-starter/package.json b/templates/docs-starter/package.json index 40c78d2..bfa058c 100644 --- a/templates/docs-starter/package.json +++ b/templates/docs-starter/package.json @@ -1,5 +1,5 @@ { - "name": "docs-starter", + "name": "__pkg_name_placeholder__", "type": "module", "private": true, "scripts": { diff --git a/templates/docs-starter/tsconfig.json b/templates/docs-starter/tsconfig.json index 1deb3e5..56cbc66 100644 --- a/templates/docs-starter/tsconfig.json +++ b/templates/docs-starter/tsconfig.json @@ -1,26 +1,26 @@ { - "compilerOptions": { - "target": "es2018", - "module": "esnext", - "lib": ["esnext"], - "moduleResolution": "node", - "esModuleInterop": true, - "strict": true, - "strictNullChecks": true, - "resolveJsonModule": true, - "skipDefaultLibCheck": true, - "preserveSymlinks": true, - "skipLibCheck": true, - "jsx": "preserve", - "types": [ - "node", - "vitepress", - "vite/client", - "vitest/importMeta" - ], - }, - "exclude": [ - "dist/**", - "node_modules/**", + "compilerOptions": { + "target": "es2018", + "module": "esnext", + "lib": ["esnext"], + "moduleResolution": "node", + "esModuleInterop": true, + "strict": true, + "strictNullChecks": true, + "resolveJsonModule": true, + "skipDefaultLibCheck": true, + "preserveSymlinks": true, + "skipLibCheck": true, + "jsx": "preserve", + "types": [ + "node", + "vitepress", + "vite/client", + "vitest/importMeta" ] - } \ No newline at end of file + }, + "exclude": [ + "dist/**", + "node_modules/**" + ] +} diff --git a/templates/ts-starter-vite/package.json b/templates/ts-starter-vite/package.json index 1e56260..56bdba0 100644 --- a/templates/ts-starter-vite/package.json +++ b/templates/ts-starter-vite/package.json @@ -1,5 +1,5 @@ { - "name": "ts-starter-vite", + "name": "__pkg_name_placeholder__", "version": "1.0.0", "packageManager": "pnpm@8.10.2", "description": "typescript-vite-template", @@ -13,12 +13,12 @@ "keywords": [ "typescript" ], - "homepage": "https://github.com/peterroe/un#readme", + "homepage": "https://github.com/peterroe/__pkg_name_placeholder__#readme", "repository": { "type": "git", - "url": "git+https://github.com/peterroe/un.git" + "url": "git+https://github.com/peterroe/__pkg_name_placeholder__.git" }, - "bugs": "https://github.com/peterroe/un/issues", + "bugs": "https://github.com/peterroe/__pkg_name_placeholder__/issues", "main": "./dist/index.umd.js", "module": "./dist/index.es.js", "types": "./dist/index.d.ts", @@ -54,5 +54,6 @@ "vite": "4.5.2", "vite-plugin-dts": "2.3.0", "vitest": "0.28.5" - } + }, + "dependencies": {} } diff --git a/templates/ts-starter/package.json b/templates/ts-starter/package.json index ecb564c..e3bdd6c 100644 --- a/templates/ts-starter/package.json +++ b/templates/ts-starter/package.json @@ -1,5 +1,5 @@ { - "name": "ts-starter", + "name": "__pkg_name_placeholder__", "version": "1.0.0", "packageManager": "pnpm@8.10.2", "description": "typescript-template", @@ -14,12 +14,12 @@ "typescript", "template" ], - "homepage": "https://github.com/peterroe/un#readme", + "homepage": "https://github.com/peterroe/__pkg_name_placeholder__#readme", "repository": { "type": "git", - "url": "git+https://github.com/peterroe/un.git" + "url": "git+https://github.com/peterroe/__pkg_name_placeholder__.git" }, - "bugs": "https://github.com/peterroe/un/issues", + "bugs": "https://github.com/peterroe/__pkg_name_placeholder__/issues", "main": "./dist/index.js", "module": "./dist/index.mjs", "types": "./dist/index.d.ts", diff --git a/templates/vue-component-starter/package.json b/templates/vue-component-starter/package.json index 22441fc..27ff4bb 100644 --- a/templates/vue-component-starter/package.json +++ b/templates/vue-component-starter/package.json @@ -1,5 +1,5 @@ { - "name": "vue-component-starter", + "name": "__pkg_name_placeholder__", "version": "1.0.0", "packageManager": "pnpm@8.10.2", "description": "component based on vue3.x", @@ -11,12 +11,12 @@ "component", "vite" ], - "homepage": "https://github.com/peterroe/un#readme", + "homepage": "https://github.com/peterroe/__pkg_name_placeholder__#readme", "repository": { "type": "git", - "url": "git+https://github.com/peterroe/un.git" + "url": "git+https://github.com/peterroe/__pkg_name_placeholder__.git" }, - "bugs": "https://github.com/peterroe/un/issues", + "bugs": "https://github.com/peterroe/__pkg_name_placeholder__/issues", "main": "./dist/index.umd.js", "module": "./dist/index.es.js", "types": "./dist/index.d.ts",