Commit ca261ebd authored by rockyl's avatar rockyl

init

parents
Pipeline #190230 failed with stages
in 0 seconds
# Created by .ignore support plugin (hsz.mobi)
### Node template
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
.env.test
# parcel-bundler cache (https://parceljs.org/)
.cache
# next.js build output
.next
# nuxt.js build output
.nuxt
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
/**
* Created by rockyl on 2019-12-25.
*/
const program = require('commander');
const {start} = require('../src/index');
program
.option('-p, --port [string]', 'Server port', '7788')
.option('-f, --file [string]', 'Target file')
.parse(process.argv);
if(!program.file){
console.error('Target file must be specified');
}else{
start(program);
}
{
"name": "code-serve",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"commander": "^4.0.1",
"fs-extra": "^8.1.0",
"socket.io": "^2.3.0"
}
}
/**
* Created by rockyl on 2019-12-25.
*/
const Server = require('socket.io');
const path = require('path');
const fs = require('fs-extra');
exports.start = function (options) {
const sockets = [];
let watching = false;
fs.watch(options.file, {}, (eventType) => {
if (watching) {
if (eventType === 'change') {
const code = fs.readFileSync(options.file, 'utf-8');
for(let socket of sockets){
socket.emit('edit-save', code);
}
}
}
});
let server = Server(options.port);
server.on('connection', function (socket) {
sockets.push(socket);
socket.on('disconnect', function () {
sockets.splice(sockets.indexOf(socket), 1);
});
socket.on('edit-open', function (data) {
fs.ensureDir(path.dirname(options.file));
watching = false;
fs.writeFileSync(options.file, data);
setTimeout(function () {
watching = true;
}, 500);
})
});
};
This diff is collapsed.
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
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