Commit 10059a8d authored by liupengfei's avatar liupengfei

create 命令

parent ed956238
/* /*
* @Author: flyharvest * @Author: flyharvest
* @Date: 2020-07-14 10:15:41 * @Date: 2020-07-14 10:15:41
* @LastEditTime: 2020-07-14 13:42:10 * @LastEditTime: 2020-07-14 13:53:31
* @LastEditors: flyharvest * @LastEditors: flyharvest
*/ */
const fs = require('fs') const fs = require('fs')
const path = require('path') const path = require('path')
const chalk = require('chalk') const log = require('./util/log.js'
)
function error (message) { function error (message) {
return new Error({ return {
message message
}) }
} }
function commandArgs (string) { function commandArgs (string) {
...@@ -46,15 +46,11 @@ function writeFile (path, content) { ...@@ -46,15 +46,11 @@ function writeFile (path, content) {
// log相关 // log相关
function buildError (reason) { function buildError (reason) {
console.log(chalk.red(`[构建错误]:\n ${reason}`)) log.error(reason)
}
function buildSuccess (success) {
console.log(chalk.green(`[构建成功]:\n ${success}`))
} }
function buildInStep (stepDes) { function buildInStep (stepDes) {
console.log(chalk.green(`[构建中...]:\n ${stepDes}`)) log.info(stepDes)
} }
const autoWriteNote = (fileName) => { const autoWriteNote = (fileName) => {
...@@ -113,12 +109,12 @@ function buildEntry () { ...@@ -113,12 +109,12 @@ function buildEntry () {
return createBuildFile(buildEntryPath, res[0], res[1]) return createBuildFile(buildEntryPath, res[0], res[1])
}) })
.then(() => { .then(() => {
buildInStep('入口文件构建完成, 开始 rollup 构建') buildInStep('入口文件构建完成, 开始webpack构建')
}) })
.catch(err => { .catch(err => {
console.log(err)
const { message = '未知错误' } = err const { message = '未知错误' } = err
buildError(message) buildError(message)
process.exit()
}) })
} }
......
/*
* @Author: flyharvest
* @Date: 2020-07-08 17:22:20
* @LastEditTime: 2020-07-14 13:47:21
* @LastEditors: flyharvest
*/
const fs = require('fs') const fs = require('fs')
const path = require('path') const path = require('path')
......
/*
* @Author: flyharvest
* @Date: 2020-07-14 13:46:01
* @LastEditTime: 2020-07-14 14:55:38
* @LastEditors: flyharvest
*/
const { log } = require('./util')
const path = require('path')
const fs = require('fs')
const { resolve } = require('path')
const { create } = require('domain')
function error (message) {
return {
message
}
}
// 检测文件
function access (path) {
return new Promise((resolve, reject) => {
fs.access(path, (err) => {
if (err) {
resolve(false)
} else {
resolve(true)
}
})
})
}
// 读取文件
function readFile (path) {
return new Promise((resolve, reject) => {
fs.readFile(path, (err, content) => {
if (err) {
reject(err)
} else {
resolve(content)
}
})
})
}
// 写入文件
function writeFile (path, content) {
return new Promise((resolve, reject) => {
fs.writeFile(path, content, { encoding: 'utf8' }, (err) => {
if (err) {
reject(error(`写入文件失败 ${path}`))
} else {
resolve(content)
}
})
})
}
// 获取仓库名称
function getLibName () {
const args = process.argv.splice(2)
const len = args.length
let lib
if (len === 0) {
log.error('[create]:需要一个参数')
process.exit()
} else if (len > 1) {
log.info('[create]:多余的参数')
lib = args[0]
} else {
lib = args[0]
}
return lib
}
// 查看是否有相应文件
function isNotFile (lib) {
const srclib = path.resolve(__dirname, '../src/', lib)
const testlib = path.resolve(__dirname, '../test/', lib)
return Promise.all([access(srclib), access(testlib)])
}
// makeDir
function mkdir (path) {
return new Promise((resolve, reject) => {
fs.mkdir(path, { recursive: true }, err => {
if (err) {
console.log(err)
reject(error('创建文件夹失败'))
} else {
resolve(true)
}
})
})
}
function makeLib (lib) {
const srclib = path.resolve(__dirname, `../src/${lib}`)
const testlib = path.resolve(__dirname, `../test/${lib}`)
return Promise.all([mkdir(srclib), mkdir(testlib)])
}
async function createLibFile (lib) {
const libTest = `../test/${lib}/`
const libSrc = `../src/${lib}/`
const testHtml = path.resolve(__dirname, libTest, 'index.html')
const testJs = path.resolve(__dirname, libTest, 'index.js')
const srcJs = path.resolve(__dirname, libSrc, 'index.js')
const srcReadMe = path.resolve(__dirname, libSrc, 'README.md')
const content = [
readFile(path.resolve(__dirname, '../test/public/index.html')),
readFile(path.resolve(__dirname, '../test/public/index.js'))
]
const [html, js] = await Promise.all(content)
const write = [
writeFile(testHtml, html),
writeFile(testJs, js),
writeFile(srcJs, ''),
writeFile(srcReadMe, '')
]
return Promise.all(write)
}
const lib = getLibName()
isNotFile(lib)
.then(res => {
console.log(res)
if (!res[0] && !res[1]) {
return true
} else if (res[0]) {
return Promise.reject(error(`test 下已有${lib}目录,请删除`))
} else if (res[1]) {
return Promise.reject(error(`src 下已有${lib}目录,请删除`))
}
})
.then(() => {
return makeLib(lib)
})
.then(() => {
return createLibFile(lib)
})
.catch(err => {
const { message } = err
log.error(message)
})
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
"module": "dist/index.es.js", "module": "dist/index.es.js",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"create": "node cli/create.js",
"serve": "cross-env NODE_ENV=development node cli/serve", "serve": "cross-env NODE_ENV=development node cli/serve",
"build": "npm run check && cross-env NODE_ENV=production node cli/build", "build": "npm run check && cross-env NODE_ENV=production node cli/build",
"check": "cross-env NODE_ENV=production node cli/check.branch", "check": "cross-env NODE_ENV=production node cli/check.branch",
......
<!-- <!--
* @Author: flyharvest * @Author: flyharvest
* @Date: 2020-07-14 10:30:48 * @Date: 2020-07-14 10:30:48
* @LastEditTime: 2020-07-14 13:33:46 * @LastEditTime: 2020-07-14 14:09:52
* @LastEditors: flyharvest * @LastEditors: flyharvest
--> -->
<!DOCTYPE html> <!DOCTYPE html>
......
/*
* @Author: flyharvest
* @Date: 2020-07-14 14:09:40
* @LastEditTime: 2020-07-14 14:10:14
* @LastEditors: flyharvest
*/
import * as rem from '@lib/flexible'
rem.setViewPort()
rem.init(750, 750)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment