Add script to create new package from template

This commit is contained in:
Jerko Steiner 2019-08-06 15:40:16 +07:00
parent 87d515d89b
commit 10ca503e59
3 changed files with 69 additions and 0 deletions

View File

@ -106,6 +106,10 @@ function getValue(
return isPositional ? argument : it.next()
}
export function isHelp(argv: string[]) {
return argv.some(a => /^(-h|--help)$/.test(a))
}
function checkChoice<T>(argument: string, choice: T, choices?: T[]) {
if (choices) {
assert(

View File

@ -1,2 +1,3 @@
export * from './help'
export * from './build'
export * from './newlib'

View File

@ -0,0 +1,64 @@
import {argparse, arg, isHelp} from '@rondo/argparse'
import {run} from '../run'
import * as fs from 'fs'
import * as path from 'path'
export async function newlib(...argv: string[]) {
const {parse, help} = argparse({
name: arg('string', {positional: true, required: true}),
namespace: arg('string', {default: '@rondo'}),
help: arg('boolean', {
alias: 'h',
description: 'Print help message',
}),
// frontend: arg('boolean', {alias: 'f'}),
})
if (isHelp(argv)) {
console.log('Usage: rondo newlib ' + help())
return
}
const args = parse(argv)
const destDir = path.join('./packages', args.name)
console.log('mkdir %s', destDir)
fs.mkdirSync(destDir)
const libraryName = `${args.namespace}/${args.name}`
const templateDir = path.join('.', 'template')
for (const file of await walk(templateDir)) {
const src = file
const dest = path.join(destDir, path.relative(templateDir, file))
if (dest === path.join(destDir, 'package.json')) {
console.log('Add %s', dest)
const libPkg = JSON.parse(fs.readFileSync(src, 'utf8'))
libPkg.name = libraryName
fs.writeFileSync(dest, JSON.stringify(libPkg, null, ' '))
} else {
console.log('Copy %s', src)
fs.copyFileSync(src, dest)
}
}
console.log('Update main package.json')
const pkgFile = path.join(process.cwd(), 'package.json')
const pkg = require(pkgFile)
pkg.dependencies = pkg.dependencies || {}
pkg.dependencies[libraryName] = `file:packages/${args.name}`
fs.writeFileSync(pkgFile, JSON.stringify(pkg, null, ' '))
}
export async function walk(
file: string,
files: string[] = [],
): Promise<string[]> {
files.push(file)
const stat = fs.statSync(file)
if (stat.isDirectory()) {
for (const f of fs.readdirSync(file)) {
walk(path.join(file, f), files)
}
}
return files
}