Skip to content

Commit

Permalink
feat: automatically replace package name
Browse files Browse the repository at this point in the history
  • Loading branch information
peterroe committed Mar 23, 2024
1 parent d24c509 commit b451c16
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 71 deletions.
39 changes: 17 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
Expand Down Expand Up @@ -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))
}
}
}
10 changes: 5 additions & 5 deletions templates/cli-starter/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand All @@ -33,7 +33,7 @@
"./*": "./*"
},
"bin": {
"cli-starter": "./bin/index.mjs"
"__pkg_name_placeholder__": "./bin/index.mjs"
},
"scripts": {
"build": "unbuild",
Expand Down
11 changes: 5 additions & 6 deletions templates/docs-starter/.vitepress/config.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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 }],
Expand Down Expand Up @@ -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.',
Expand Down
2 changes: 1 addition & 1 deletion templates/docs-starter/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "docs-starter",
"name": "__pkg_name_placeholder__",
"type": "module",
"private": true,
"scripts": {
Expand Down
48 changes: 24 additions & 24 deletions templates/docs-starter/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
},
"exclude": [
"dist/**",
"node_modules/**"
]
}
11 changes: 6 additions & 5 deletions templates/ts-starter-vite/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -54,5 +54,6 @@
"vite": "4.5.2",
"vite-plugin-dts": "2.3.0",
"vitest": "0.28.5"
}
},
"dependencies": {}
}
8 changes: 4 additions & 4 deletions templates/ts-starter/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "ts-starter",
"name": "__pkg_name_placeholder__",
"version": "1.0.0",
"packageManager": "pnpm@8.10.2",
"description": "typescript-template",
Expand All @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions templates/vue-component-starter/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit b451c16

Please sign in to comment.