rename dir

This commit is contained in:
pengfei.zhou
2019-12-21 21:56:43 +08:00
parent 1d40c1e1e0
commit 6b68d8472c
67 changed files with 0 additions and 32 deletions

View File

@@ -0,0 +1,53 @@
require('shelljs/global')
const fs = require("fs")
const path = require("path")
const SourceMapMerger = require("source-map-merger");
function fromDir(startPath, filter) {
if (!fs.existsSync(startPath)) {
console.log("no dir ", startPath);
return;
}
const files = fs.readdirSync(startPath);
for (let i = 0; i < files.length; i++) {
const filename = path.join(startPath, files[i]);
const stat = fs.lstatSync(filename);
if (stat.isDirectory()) {
fromDir(filename, filter);
}
else if (filename.indexOf(filter) >= 0) {
try {
doMerge(startPath, files[i])
} catch (e) {
console.log(e)
}
};
};
};
function doMerge(startPath, fileName) {
const filePath = fileName ? path.join(startPath, fileName) : startPath
const mergedMap = SourceMapMerger.createMergedSourceMapFromFiles([
filePath.replace(/bundle\//, 'build/'),
filePath,
], true);
fs.writeFileSync(filePath, mergedMap)
return mergedMap
}
function mergeMappings() {
fromDir("bundle", ".map")
}
module.exports = {
build: () => {
exec('npm run build')
console.log('Deal mapping')
mergeMappings()
},
clean: () => {
exec('npm run clean')
},
mergeMappings,
doMerge,
}

140
doric-cli/scripts/init.js Normal file
View File

@@ -0,0 +1,140 @@
var fs = require('fs');
var path = require('path')
require('shelljs/global')
const targetJSPath = `${__dirname}/../target/js`
const targetAndroidPath = `${__dirname}/../target/android`
const targetiOSPath = `${__dirname}/../target/iOS`
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) {
if (fs.existsSync(path)) {
console.warn(`Dir:${process.cwd()}/${path} already exists`)
return;
}
fs.mkdir(path, function (err) {
if (err) {
return console.error(err);
}
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`)
})
})
}
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`)
})
})
}
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'].forEach(e => {
fs.writeFileSync(`${path}/${e}`, fs.readFileSync(`${targetiOSPath}/${e}`).toString().replace(/__\$__/g, name))
})
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}/js`, name)
initAndroid(`${process.cwd()}/${name}/android`, name)
initiOS(`${process.cwd()}/${name}/iOS`, name)
})
}

View File

@@ -0,0 +1,70 @@
const ws = require('nodejs-websocket')
const { exec, spawn } = require('child_process')
const fs = require('fs')
var server
var contextId = null
var clientConnection = null
var debuggerConnection = null
const createServer = () => {
server = ws.createServer(connection => {
console.log('connected', connection.headers.host)
if (connection.headers.host.startsWith("localhost")) {
console.log("debugger " + connection.key + " attached to dev kit")
debuggerConnection = connection
clientConnection.sendText(JSON.stringify({
cmd: 'SWITCH_TO_DEBUG',
contextId: contextId
}), function() {
})
} else {
console.log("client " + connection.key + " attached to dev kit")
}
connection.on('text', function (result) {
console.log('text', result)
let resultObject = JSON.parse(result)
switch(resultObject.cmd) {
case 'DEBUG':
clientConnection = connection
server.debugging = true
console.log("enter debugging")
contextId = resultObject.data.contextId
let projectHome = '.'
fs.writeFileSync(projectHome + '/build/context', contextId, 'utf8')
let source = resultObject.data.source
console.log(connection.key + " request debug, project home: " + projectHome)
spawn('code', [projectHome, projectHome + "/src/" + source])
setTimeout(() => {
exec('osascript -e \'tell application "System Events"\ntell application "Visual Studio Code" to activate\nkey code 96\nend tell\'', (err, stdout, stderr) => {
if (err) {
console.log(`stdout: ${err}`)
}
})
}, 1500)
break
}
})
connection.on('connect', function (code) {
console.log('connect', code)
})
connection.on('close', function (code) {
console.log('close: code = ' + code, connection.key)
console.log("quit debugging")
server.debugging = false
})
connection.on('error', function (code) {
console.log('error', code)
})
})
return server
}
module.exports = createServer()

View File

@@ -0,0 +1,82 @@
const chokidar = require('chokidar')
const ws = require('./server')
const fs = require("fs")
const doMerge = require("./command").doMerge
require('shelljs/global')
exec('npm run dev >/dev/null 2>&1', { async: true })
console.warn('Waiting ...')
setTimeout(() => {
console.warn('Start watching')
ws.listen(7777)
chokidar.watch(process.cwd() + "/bundle", {
ignored: /(^|[\/\\])\../,
}).on('change', (path) => {
if (ws.debugging) {
console.log("debugging, hot reload by pass")
return
}
fs.readFile(path, 'utf-8', (error, data) => {
if (!path.endsWith('.map')) {
console.log('File change:', path)
try {
const sourceMap = doMerge(path + ".map")
ws.connections.forEach(e => {
e.sendText(JSON.stringify({
cmd: 'RELOAD',
script: data,
source: path.match(/[^/\\]*$/)[0],
sourceMap,
}))
})
} catch (e) {
console.error(e)
}
}
})
});
}, 3000);
const os = require('os');
function getIPAdress() {
const ret = []
var interfaces = os.networkInterfaces();
for (var devName in interfaces) {
var iface = interfaces[devName];
for (var i = 0; i < iface.length; i++) {
var alias = iface[i];
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
ret.push(alias.address);
}
}
}
return ret
}
const qrcode = require('qrcode-terminal');
const ips = getIPAdress()
ips.forEach(e => {
console.log(`IP:${e}`)
qrcode.generate(e, { small: false });
})
const keypress = require('keypress');
keypress(process.stdin);
process.stdin.on('keypress', function (ch, key) {
if (key && key.ctrl && key.name == 'r') {
ips.forEach(e => {
console.log(`IP:${e}`)
qrcode.generate(e, { small: false });
})
}
if (key && key.ctrl && key.name == 'c') {
process.stdin.pause();
process.exit(0);
}
});
process.stdin.setRawMode(true);
process.stdin.resume();