Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@
"chokidar": "^3.0.1",
"commander": "^2.20.0",
"form-data": "^2.4.0",
"fs-extra": "^8.1.0",
"got": "^9.6.0",
"lodash": "^4.17.11",
"lodash": "^4.17.15",
"lowdb": "^1.0.0"
}
}
39 changes: 39 additions & 0 deletions src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as chokidar from 'chokidar'
import * as _ from 'lodash'
import * as got from 'got'
import * as FormData from 'form-data'
import * as fse from 'fs-extra'

export function showHost () {
const ip = getHost()
Expand Down Expand Up @@ -105,3 +106,41 @@ export async function build (path: string, ouputPath?: string) {
await zipFolder(path, ouputPath)
log.info(`Build in ${ouputPath}`)
}

export async function create (path: string, packageName?: string) {
const createInCurDir = !packageName
const sourcePath = resolve(__dirname, '../template')
const targetPath = createInCurDir ? path : resolve(path, packageName)
const targetConfigPath = resolve(targetPath, 'config.json')
const filterFiles = ['.gitkeep']
packageName = createInCurDir ? basename(targetPath) : packageName

if (createInCurDir) {
if (fs.readdirSync(targetPath).length) {
log.error(`Current directory is not empty`)
process.exit(1)
}
} else if (fs.existsSync(targetPath)) {
log.error(`${targetPath} is already exist, try another name`)
process.exit(1)
}

const [, err] = await tryCatch(fse.copy(sourcePath, targetPath, {
filter: (_, dest) => {
return !filterFiles.includes(basename(dest))
}
}))
if (err) {
log.error('Create package failed')
process.exit(1)
}

// It doesn't matter if modification of config's name field failed
const [configObj] = await tryCatch<object>(fse.readJson(targetConfigPath))
if (configObj) {
_.set(configObj, 'info.name', packageName)
await tryCatch(fse.writeJson(targetConfigPath, configObj, { spaces: 2 }))
}

log.info(`Create package ${packageName} success`)
}
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as program from 'commander'
import * as net from 'net'
import * as path from 'path'
import { showHost, watch, saveHost, build } from './actions'
import { showHost, watch, saveHost, build, create } from './actions'
import * as log from './log'

program
Expand Down Expand Up @@ -48,4 +48,12 @@ program
await build(dir, cmd.output)
})

program
.command('create [name]')
.description('Create a new box package')
.action(async (packageName: string) => {
const pwd = process.cwd()
await create(pwd, packageName)
})

program.parse(process.argv)
Empty file added template/README.md
Empty file.
Empty file added template/assets/.gitkeep
Empty file.
18 changes: 18 additions & 0 deletions template/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"info": {
"name": "",
"url": "",
"version": "1.0.0",
"author": "",
"website": "",
"types": 0
},
"settings": {
"minSDKVer": "1.0.0",
"minOSVer": "10.0.0",
"idleTimerDisabled": false,
"autoKeyboardEnabled": false,
"keyboardToolbarEnabled": false,
"rotateDisabled": false
}
}
Empty file added template/main.js
Empty file.
Empty file added template/scripts/.gitkeep
Empty file.
Empty file added template/strings/.gitkeep
Empty file.