var fs = require('fs'); var path = require('path') require('shelljs/global') const targetJSPath = `${__dirname}/../target/` const targetAndroidPath = `${__dirname}/../target/android` const targetiOSPath = `${__dirname}/../target/iOS` const currentVersion = fs.readFileSync(`${targetJSPath}/version`).toString().trim() 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) { console.log(`create dir ${path} success`); fs.writeFileSync(`${path}/package.json`, fs.readFileSync(`${targetJSPath}/_package.json`).toString() .replace(/__\$__/g, name).replace(/__\$Version__/g, currentVersion)) 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`) }) } 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}`, () => { [ 'app/src/main/java/pub/doric/example/MainActivity.java', 'app/build.gradle', 'app/src/main/res/values/strings.xml', 'settings.gradle', ].forEach(e => { fs.writeFileSync(`${path}/${e}`, fs.readFileSync(`${targetAndroidPath}/${e}`).toString() .replace(/__\$__/g, name) .replace(/__\$Version__/g, currentVersion)) }) console.log(`Create Doric Android Project Success`) }) }) } 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}`, () => { [ 'Example/SceneDelegate.m', 'Example/AppDelegate.m', 'Podfile', ].forEach(e => { fs.writeFileSync(`${pathq}/${e}`, fs.readFileSync(`${targetiOSPath}/${e}`).toString() .replace(/__\$__/g, name) .replace(/__\$Version__/g, currentVersion)) }) console.log(`Create Doric iOS Project Success`) }) }) } 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); } initJS(`${process.cwd()}/${name}`, name) initAndroid(`${process.cwd()}/${name}/android`, name) initiOS(`${process.cwd()}/${name}/iOS`, name) }) }