From 927820a5ecddaddca7f57b073e0e5f7047faef34 Mon Sep 17 00:00:00 2001 From: calebboyd Date: Mon, 2 Oct 2017 18:02:32 -0500 Subject: [PATCH] chore: service progress --- README.md | 5 +- plugins/nexe-daemon/package.json | 8 +-- plugins/nexe-daemon/src/index.ts | 7 ++- plugins/nexe-daemon/src/nexe-daemon.ts | 71 ++++++++++++++++++++++++-- plugins/nexe-daemon/yarn.lock | 6 +-- src/options.ts | 4 +- 6 files changed, 83 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 1aa7348..c94062e 100644 --- a/README.md +++ b/README.md @@ -129,7 +129,7 @@ compile({ - #### `make: string[]` - Array of arguments for the node build make step, on windows this step recieves options for vcBuild.bat - default: `[]` or `['nosign', 'release']` for non windows systems - - #### `make: string[]` + - #### `vcBuild: string[]` - Alias for `make` option - #### `snapshot: string` - path to a file to be used as the warmup snapshot for the build @@ -180,8 +180,7 @@ compile({ Patches and Plugins are just a middleware functions that take two arguments, the `compiler`, and `next`. The compiler is described below, and `next` ensures that the pipeline continues. Its invocation should always be awaited or returned to ensure correct behavior. Patches also require that [`--build`](#build-boolean) be set, while plugins do not. -For examples, see the built in patches: [src/patches](src/patches) -A plugin +For examples, see the built in patches: [src/patches](src/patches). ### `NexeCompiler` diff --git a/plugins/nexe-daemon/package.json b/plugins/nexe-daemon/package.json index 8f2a55c..d0bebb9 100644 --- a/plugins/nexe-daemon/package.json +++ b/plugins/nexe-daemon/package.json @@ -2,21 +2,23 @@ "name": "nexe-daemon", "version": "1.0.0", "description": "Plugin for nexe to create installable services", - "main": "index.js", + "main": "lib/index.js", "scripts": { "clean": "rimraf *.js", "build": "tsc", "postpublish": "npm run clean" }, "files": [ - "*.js", + "lib", "winsw.exe" ], "author": "Caleb Boyd", "license": "MIT", "devDependencies": { - "nexe": "^2.0.0-rc.6", "rimraf": "^2.6.2", "typescript": "^2.5.2" + }, + "dependencies": { + "nexe": "^2.0.0-rc.11" } } diff --git a/plugins/nexe-daemon/src/index.ts b/plugins/nexe-daemon/src/index.ts index be43fb2..00a3f17 100644 --- a/plugins/nexe-daemon/src/index.ts +++ b/plugins/nexe-daemon/src/index.ts @@ -22,13 +22,12 @@ export default function daemon (compiler: NexeCompiler, next: () if (compiler.target.platform !== 'windows') { return next() } - compiler.addResource( './nexe/plugin/daemon/winsw.exe', - readFileSync(require.resolve('./winsw.exe')) + readFileSync(require.resolve('../winsw.exe')) ) const name = compiler.options.name, - options = compiler.options.daemon.windows, + options = compiler.options.daemon && compiler.options.daemon.windows || {}, defaults: NexeDaemonOptions = { id: name, name, @@ -37,7 +36,7 @@ export default function daemon (compiler: NexeCompiler, next: () } compiler.addResource( - './nexe/plugin/daemon/winsw-config.xml', + './nexe/plugin/daemon/winsw.xml', Buffer.from(renderWinswConfig(Object.assign(defaults, options))) ) compiler.addResource( diff --git a/plugins/nexe-daemon/src/nexe-daemon.ts b/plugins/nexe-daemon/src/nexe-daemon.ts index c034da6..63c30b3 100644 --- a/plugins/nexe-daemon/src/nexe-daemon.ts +++ b/plugins/nexe-daemon/src/nexe-daemon.ts @@ -1,4 +1,69 @@ -console.log('hello world') -if (true) { - console.log('wat') +const installCommands = ['-i', '--install'] +const installIndex = process.argv.findIndex(x => installCommands.includes(x)) +import * as path from 'path' +import * as fs from 'fs' +import * as cp from 'child_process' + +function mkdirp (r: string, t?: any): any { + ;(t = t || null), (r = path.resolve(r)) + try { + fs.mkdirSync(r), (t = t || r) + } catch (c) { + if ('ENOENT' === c.code) (t = mkdirp(path.dirname(r), t)), mkdirp(r, t) + else { + var i + try { + i = fs.statSync(r) + } catch (r) { + throw c + } + if (!i.isDirectory()) throw c + } + } + return t +} + +if (~installIndex) { + console.log('DIRECTORY FOUND') + const directory = process.argv[installIndex + 1] + if (directory) { + mkdirp(path.dirname(directory)) + const filename = path.join(directory, path.basename(process.execPath)) + const readStream = fs.createReadStream(process.execPath) + const writeStream = fs.createWriteStream(filename) + + const onError = function () { + readStream.removeAllListeners() + writeStream.removeAllListeners() + //TODO + } + + readStream + .on('error', onError) + .pipe(writeStream) + .on('error', onError) + .on('close', () => { + const winsw = filename.replace('.exe', '-service.exe') + fs.writeFileSync( + winsw, + fs.readFileSync('./nexe/plugin/daemon/winsw.exe') + ) + fs.writeFileSync( + filename.replace('.exe', '-service.xml'), + fs.readFileSync('./nexe/plugin/daemon/winsw.xml') + ) + installService(winsw) + }) + } +} else { + console.log("DIRECTORY NOT FOUND") + require('./nexe/plugin/daemon/app.js') +} + +function installService (filename: string) { + cp.exec(filename + ' install', (error) => { + if (error && !error.message.includes('already exists')) { + throw error + } + }) } diff --git a/plugins/nexe-daemon/yarn.lock b/plugins/nexe-daemon/yarn.lock index 0f5a645..894c07e 100644 --- a/plugins/nexe-daemon/yarn.lock +++ b/plugins/nexe-daemon/yarn.lock @@ -1458,9 +1458,9 @@ negotiator@0.6.1: version "0.6.1" resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.1.tgz#2b327184e8992101177b28563fb5e7102acd0ca9" -nexe@^2.0.0-rc.6: - version "2.0.0-rc.6" - resolved "https://registry.yarnpkg.com/nexe/-/nexe-2.0.0-rc.6.tgz#b3990bb67098a93bc488b21ad88aee67b62cf0d5" +nexe@^2.0.0-rc.11: + version "2.0.0-rc.11" + resolved "https://registry.yarnpkg.com/nexe/-/nexe-2.0.0-rc.11.tgz#cd06fc91da8db008fffa6dde1a9a8dd6decf2c35" dependencies: app-builder "^5.1.0" chalk "^1.1.3" diff --git a/src/options.ts b/src/options.ts index 700d269..5363a30 100644 --- a/src/options.ts +++ b/src/options.ts @@ -241,8 +241,8 @@ function normalizeOptionsAsync(input?: Partial): Promise k !== 'rc')