2019-12-04 14:16:57 +08:00
|
|
|
var fs = require('fs');
|
2019-12-05 20:52:38 +08:00
|
|
|
var path = require('path')
|
|
|
|
|
2019-12-04 14:16:57 +08:00
|
|
|
require('shelljs/global')
|
|
|
|
|
2020-01-07 14:03:07 +08:00
|
|
|
const targetJSPath = `${__dirname}/../target/`
|
2019-12-05 20:52:38 +08:00
|
|
|
const targetAndroidPath = `${__dirname}/../target/android`
|
2019-12-05 21:15:16 +08:00
|
|
|
const targetiOSPath = `${__dirname}/../target/iOS`
|
2019-12-05 20:52:38 +08:00
|
|
|
|
|
|
|
function copyFile(srcPath, tarPath, cb) {
|
|
|
|
var rs = fs.createReadStream(srcPath)
|
|
|
|
rs.on('error', function (err) {
|
|
|
|
if (err) {
|
|
|
|
console.log('read error', srcPath)
|
|
|
|
}
|
|
|
|
cb && cb(err)
|
|
|
|
})
|
|
|
|
|
|
|
|
var ws = fs.createWriteStream(tarPath)
|
|
|
|
ws.on('error', function (err) {
|
|
|
|
if (err) {
|
|
|
|
console.log('write error', tarPath)
|
|
|
|
}
|
|
|
|
cb && cb(err)
|
|
|
|
})
|
|
|
|
ws.on('close', function (ex) {
|
|
|
|
cb && cb(ex)
|
|
|
|
})
|
|
|
|
|
|
|
|
rs.pipe(ws)
|
|
|
|
}
|
|
|
|
|
|
|
|
function copyFolder(srcDir, tarDir, cb) {
|
|
|
|
fs.readdir(srcDir, function (err, files) {
|
|
|
|
var count = 0
|
|
|
|
var checkEnd = function () {
|
|
|
|
++count == files.length && cb && cb()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
checkEnd()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
files.forEach(function (file) {
|
|
|
|
var srcPath = path.join(srcDir, file)
|
|
|
|
var tarPath = path.join(tarDir, file)
|
|
|
|
|
|
|
|
fs.stat(srcPath, function (err, stats) {
|
|
|
|
if (stats.isDirectory()) {
|
|
|
|
fs.mkdir(tarPath, function (err) {
|
|
|
|
if (err) {
|
|
|
|
console.log(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
copyFolder(srcPath, tarPath, checkEnd)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
copyFile(srcPath, tarPath, checkEnd)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
files.length === 0 && cb && cb()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function initJS(path, name) {
|
2020-01-07 14:03:07 +08:00
|
|
|
console.log(`create dir ${path} success`);
|
|
|
|
fs.writeFileSync(`${path}/package.json`, fs.readFileSync(`${targetJSPath}/_package.json`).toString().replace(/__\$__/g, name))
|
|
|
|
fs.writeFileSync(`${path}/tsconfig.json`, fs.readFileSync(`${targetJSPath}/_tsconfig.json`))
|
|
|
|
fs.writeFileSync(`${path}/rollup.config.js`, fs.readFileSync(`${targetJSPath}/_rollup.config.js`))
|
|
|
|
fs.writeFileSync(`${path}/.gitignore`, fs.readFileSync(`${targetJSPath}/_gitignore`))
|
|
|
|
fs.mkdirSync(`${path}/.vscode`)
|
|
|
|
fs.writeFileSync(`${path}/.vscode/launch.json`, fs.readFileSync(`${targetJSPath}/_launch.json`).toString().replace(/__\$__/g, name))
|
|
|
|
fs.writeFileSync(`${path}/.vscode/tasks.json`, fs.readFileSync(`${targetJSPath}/_tasks.json`).toString().replace(/__\$__/g, name))
|
|
|
|
fs.mkdirSync(`${path}/src`)
|
|
|
|
fs.writeFileSync(`${path}/src/${name}.ts`, fs.readFileSync(`${targetJSPath}/$.ts`).toString().replace(/__\$__/g, name))
|
|
|
|
fs.writeFileSync(`${path}/index.ts`, `export default ['src/${name}']`)
|
|
|
|
exec(`cd ${path} && npm install && npm run build`, () => {
|
|
|
|
console.log(`Create Doric JS Project Success`)
|
2019-12-05 20:52:38 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
function initAndroid(path, name) {
|
|
|
|
if (fs.existsSync(path)) {
|
|
|
|
console.warn(`Dir:${process.cwd()}/${path} already exists`)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log(`create dir ${path} success`);
|
|
|
|
fs.mkdir(path, function (err) {
|
|
|
|
if (err) {
|
|
|
|
return console.error(err);
|
|
|
|
}
|
|
|
|
copyFolder(`${targetAndroidPath}`, `${path}`, () => {
|
|
|
|
const mainFiles = `app/src/main/java/pub/doric/example/MainActivity.java`
|
|
|
|
fs.writeFileSync(`${path}/${mainFiles}`, fs.readFileSync(`${targetAndroidPath}/${mainFiles}`).toString().replace(/__\$__/g, name))
|
|
|
|
console.log(`Create Doric Android Project Success`)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2019-12-05 21:15:16 +08:00
|
|
|
function initiOS(path, name) {
|
|
|
|
if (fs.existsSync(path)) {
|
|
|
|
console.warn(`Dir:${process.cwd()}/${path} already exists`)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log(`create dir ${path} success`);
|
|
|
|
fs.mkdir(path, function (err) {
|
|
|
|
if (err) {
|
|
|
|
return console.error(err);
|
|
|
|
}
|
|
|
|
copyFolder(`${targetiOSPath}`, `${path}`, () => {
|
2019-12-09 14:10:45 +08:00
|
|
|
['Example/SceneDelegate.m', 'Example/AppDelegate.m'].forEach(e => {
|
|
|
|
fs.writeFileSync(`${path}/${e}`, fs.readFileSync(`${targetiOSPath}/${e}`).toString().replace(/__\$__/g, name))
|
|
|
|
})
|
2019-12-06 09:42:32 +08:00
|
|
|
console.log(`Create Doric iOS Project Success`)
|
2019-12-05 21:15:16 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2019-12-04 14:16:57 +08:00
|
|
|
module.exports = function (name) {
|
|
|
|
if (fs.existsSync(name)) {
|
|
|
|
console.warn(`Dir:${process.cwd()}/${name} already exists`)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fs.mkdir(name, function (err) {
|
|
|
|
if (err) {
|
|
|
|
return console.error(err);
|
|
|
|
}
|
2020-01-07 14:03:07 +08:00
|
|
|
initJS(`${process.cwd()}/${name}`, name)
|
2019-12-05 20:52:38 +08:00
|
|
|
initAndroid(`${process.cwd()}/${name}/android`, name)
|
2019-12-05 21:15:16 +08:00
|
|
|
initiOS(`${process.cwd()}/${name}/iOS`, name)
|
2019-12-04 14:16:57 +08:00
|
|
|
})
|
|
|
|
}
|