refactor: remove scripts dir

This commit is contained in:
pengzhanbo 2024-01-07 01:45:23 +08:00
parent 91dd748c24
commit d5306d9a49
17 changed files with 0 additions and 374 deletions

View File

@ -1,16 +0,0 @@
## scripts/create
`packages/` 目录下生成一个新的 插件包
``` sh
pnpm pkg <package-name> [--option]
```
- `package-name` 插件名称
### Option
- `--client,-c` 是否生成 `client` 目录及其文件,默认不生成
- `--shared,-s` 是否生成 `shared` 目录及其文件,默认不生成
- `--help,-h` 显示帮助信息

View File

@ -1,97 +0,0 @@
import path from 'node:path'
import fs from 'node:fs'
import { fileURLToPath } from 'node:url'
import handlebars from 'handlebars'
import chalk from 'chalk'
import ora from 'ora'
import { execa } from 'execa'
import { readTemplateList } from './readTpl.js'
import { lowerCase, packageName, upperCase } from './utils.js'
import { writeFile } from './writeFile.js'
const compile = handlebars.compile
const _dirname
= typeof __dirname !== 'undefined'
? __dirname
: path.dirname(fileURLToPath(import.meta.url))
const packagesRoot = path.join(_dirname, '../../packages')
const spinner = ora()
const pkg = JSON.parse(
fs.readFileSync(path.join(_dirname, '../../package.json'), 'utf-8'),
)
async function generatorFile(config) {
const templateList = readTemplateList(path.join(_dirname, './template'))
const { name, client, shared } = config
const pkgName = packageName(name)
const targetDir = path.join(packagesRoot, pkgName)
const data = {
pkgName,
upperName: upperCase(name),
lowerName: lowerCase(name),
client,
shared,
version: pkg.version,
}
const include = [
!client && 'client',
!shared && 'shared',
!shared && 'client/index.js',
]
.filter(Boolean)
.join('|')
const filterRE = new RegExp(`/(${include})/`)
const templates = templateList
.filter(({ file }) => {
return !filterRE.test(file)
})
.map(({ file, content }) => {
return {
file,
template: compile(content),
}
})
spinner.start(`${chalk.cyan(pkgName)} generating....`)
templates.forEach(async ({ file, template }) => {
try {
const filepath = path.join(targetDir, file)
await writeFile(filepath, template(data))
}
catch (e) {
spinner.fail(`Failed to generate ${chalk.cyan(pkgName)}`)
throw e
}
})
spinner.succeed(`${chalk.cyan(pkgName)} generated !`)
}
async function initPackage(config) {
const { name, client } = config
const pkgName = packageName(name)
const targetDir = path.join(packagesRoot, pkgName)
const dependencies = [
'@vuepress/core@next',
'@vuepress/utils@next',
'@vuepress/shared@next',
]
client && dependencies.push('@vuepress/client@next')
spinner.start(chalk.cyan('Installing...'))
try {
await execa('pnpm', ['add', ...dependencies], { cwd: targetDir })
spinner.succeed('Installed.')
}
catch (e) {
spinner.fail('Failed to Installed')
throw e
}
}
export async function generator(config) {
await generatorFile(config)
await initPackage(config)
}

View File

@ -1,29 +0,0 @@
import process from 'node:process'
import minimist from 'minimist'
const defaultOptions = {
s: false,
shared: false,
c: false,
client: false,
h: false,
help: false,
}
function normalizeArgv(argv) {
return {
name: argv._[0] || '',
client: argv.client || argv.c,
shared: argv.shared || argv.s,
help: argv.h || argv.help,
}
}
export function getConfig() {
const argv = Object.assign(
{},
defaultOptions,
minimist(process.argv.slice(2)),
)
return normalizeArgv(argv)
}

View File

@ -1,18 +0,0 @@
import chalk from 'chalk'
export function getHelp() {
console.log(` this command will generator a package to ${chalk.cyan('packages')}.
command: ${chalk.green('pnpm pkg <package-name> [--options]')}
${chalk.green('package-name')}: 包名
options:
${chalk.green('--client, -c')}: 是否生成 ${chalk.cyan('client/')} 目录
${chalk.green('--shared, -s')}: 是否生成 ${chalk.cyan('shared/')} 目录
${chalk.green('--help, -h')}: show help message.
exp: ${chalk.green('pnpm pkg caniuse -c -s')}
It will generator to ${chalk.cyan('packages/plugin-caniuse')}
`)
}

View File

@ -1,17 +0,0 @@
import process from 'node:process'
import { getConfig } from './getConfig.js'
import { getHelp } from './getHelp.js'
import { generator } from './generator.js'
const config = getConfig()
if (config.help) {
getHelp()
process.exit(0)
}
else {
generator(config).catch((err) => {
console.error(err)
process.exit(1)
})
}

View File

@ -1,33 +0,0 @@
import fs from 'node:fs'
import path from 'node:path'
const tplRE = /\.tpl$/
function readFileList(dir, fileList = {}) {
const files = fs.readdirSync(dir)
files.forEach((file) => {
const filepath = path.join(dir, file)
const stat = fs.statSync(filepath)
if (stat.isDirectory()) {
readFileList(filepath, fileList)
}
else {
const extname = path.extname(filepath)
if (tplRE.test(extname))
fileList[filepath.replace(tplRE, '')] = fs.readFileSync(filepath, 'utf-8')
}
})
return fileList
}
export function readTemplateList(dir) {
const templateMap = readFileList(dir)
const result = []
Object.keys(templateMap).forEach((key) => {
const file = path.relative(dir, key)
result.push({
file,
content: templateMap[key],
})
})
return result
}

View File

@ -1,21 +0,0 @@
The MIT License (MIT)
Copyright (C) 2021 - PRESENT by pengzhanbo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@ -1,18 +0,0 @@
# `@vuepress-plume/{{ pkgName }}`
## Install
```
yarn add @vuepress-plume/{{ pkgName }}
```
## Usage
``` js
// .vuepress/config.js
const {{ lowerName }}Plugin = require('@vuepress-plume/{{ pkgName }}')
module.exports = {
//...
plugins: [
{{ lowerName }}Plugin()
]
// ...
}
```

View File

@ -1,43 +0,0 @@
{
"name": "@vuepress-plume/{{ pkgName }}",
"version": "{{ version }}",
"description": "The Plugin for VuePres 2",
"keyword": [
"VuePress",
"vuepress plugin",
"{{ lowerName }}",
"vuepress-plugin-{{ pkgName }}"
],
"homepage": "https://github.com/pengzhanbo/vuepress-theme-plume#readme",
"bugs": {
"url": "https://github.com/pengzhanbo/vuepress-theme-plume/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/pengzhanbo/vuepress-theme-plume.git"
},
"license": "MIT",
"author": "pengzhanbo <volodymyr@foxmail.com>",
"type": "module",
"exports": {
".": "./lib/node/index.js",
{{#if shared }}
"./client": "./lib/client/index.js",
{{/if}}
"./package.json": "./package.json"
},
"main": "lib/node/index.js",
"types": "./lib/node/index.d.ts",
"scripts": {
"build": "pnpm run clean && pnpm run copy && pnpm run ts",
"clean": "rimraf lib *.tsbuildinfo",
"copy": "cpx \"src/**/*.{d.ts,vue,css,scss,jpg,png}\" lib",
"copy:watch": "cpx \"src/**/*.{d.ts,vue,css,scss,jpg,png}\" lib -w",
"dev": "concurrently \"pnpm copy:watch\" \"pnpm ts:watch\"",
"ts": "tsc -b tsconfig.build.json",
"ts:watch": "tsc -b tsconfig.build.json --watch"
},
"publishConfig": {
"access": "public"
}
}

View File

@ -1,8 +0,0 @@
import { defineClientConfig } from '@vuepress/client'
export default defineClientConfig({
setup() {
// do something
},
})

View File

@ -1 +0,0 @@
export * from '../shared/index.js'

View File

@ -1,8 +0,0 @@
import { {{ lowerName }}Plugin } from './plugin.js'
export * from './plugin.js'
{{#if shared }}
export * from '../shared/index.js'
{{/if}}
export default {{ lowerName }}Plugin

View File

@ -1,23 +0,0 @@
import type { App, Plugin } from '@vuepress/core'
{{#if client}}
import { path } from '@vuepress/utils'
{{/if}}
{{#if shared}}
import type { {{ upperName }}Options } from '../shared/index.js'
{{else}}
export interface {{ upperName }}Options {
a?: string
}
{{/if}}
export const {{ lowerName }}Plugin = (options: {{ upperName }}Options): Plugin => {
return (app: App) => {
return {
name: '@vuepress-plume/{{ pkgName }}',
{{#if client}}
clientConfigFile: path.resolve(__dirname, '../client/clientConfig.js'),
{{/if}}
}
}
}

View File

@ -1,3 +0,0 @@
export interface {{ upperName }}Options {
a?: string
}

View File

@ -1,8 +0,0 @@
{
"extends": "../tsconfig.build.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./lib"
},
"include": ["./src"]
}

View File

@ -1,14 +0,0 @@
export function upperCase(str) {
return str.split(/-|\s+/).filter(Boolean).map((s) => {
return s[0].toUpperCase() + s.slice(1)
}).join('')
}
export function lowerCase(str) {
str = upperCase(str)
return str[0].toLowerCase() + str.slice(1)
}
export function packageName(name) {
return `plugin-${name.trim().split(/-|\s+/).filter(Boolean).join('-')}`
}

View File

@ -1,17 +0,0 @@
import path from 'node:path'
import fs from 'node:fs'
export async function writeFile(filepath, content) {
const dirname = path.dirname(filepath)
if (!fs.existsSync(dirname))
fs.mkdirSync(dirname, { recursive: true })
return new Promise((resolve, reject) => {
fs.writeFile(filepath, content, 'utf-8', (err) => {
if (err)
reject(err)
else
resolve()
})
})
}