From 400e45f24a84a17f70316d00f7ed502d28682e87 Mon Sep 17 00:00:00 2001 From: Ian MacLeod Date: Mon, 31 Jul 2017 17:38:08 -0700 Subject: [PATCH] Convert the postinstall script to .js for windows support --- .gitignore | 1 + .vscode/settings.json | 2 +- package.json | 3 ++- scripts/postinstall.js | 58 ++++++++++++++++++++++++++++++++++++++++++ scripts/postinstall.sh | 23 ----------------- 5 files changed, 62 insertions(+), 25 deletions(-) create mode 100755 scripts/postinstall.js delete mode 100755 scripts/postinstall.sh diff --git a/.gitignore b/.gitignore index c6f568c4..7fc9d5f6 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ package-lock.json /output/ *.d.ts *.js +!/scripts/*.js # TypeScript !/typings/**/*.d.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index f24188ef..d42f4935 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,7 +1,6 @@ // Place your settings in this file to overwrite default and user settings. { "files.exclude": { - "**/*.js": true, "**/*.js.map": true, "**/.DS_Store": true, "**/.git": true, @@ -9,6 +8,7 @@ "**/.svn": true, "**/CVS": true, "{src,test}/**/*.d.ts": true, + "{src,test}/**/*.js": true, "node_modules/": true, "output/": true } diff --git a/package.json b/package.json index 96e4dcf4..a3fdf6da 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "dev": "./scripts/dev.sh", "fix:style": "./scripts/fix:style.sh", "greenkeeper:lockfile": "./scripts/greenkeeper:lockfile.sh", - "postinstall": "./scripts/postinstall.sh", + "postinstall": "node ./scripts/postinstall.js", "release": "./scripts/release.sh", "test": "./scripts/test.sh", "test:compile": "./scripts/test:compile.sh", @@ -48,6 +48,7 @@ "greenkeeper-lockfile": "^1.7.2", "jest": "^20.0.4", "jest-junit": "^2.1.0", + "rimraf": "^2.6.1", "typescript": "^2.4.2", "typescript-eslint-parser": "eslint/typescript-eslint-parser#ts-2.4" } diff --git a/scripts/postinstall.js b/scripts/postinstall.js new file mode 100755 index 00000000..52199ff5 --- /dev/null +++ b/scripts/postinstall.js @@ -0,0 +1,58 @@ +#!/usr/bin/env node +/* eslint-disable */ +var childProcess = require('child_process'); +var fs = require('fs'); +var path = require('path'); + +var node_modules = path.resolve(__dirname, '..', 'node_modules'); +var temp_modules = node_modules + '.tmp'; +// We set an environment flag to ensure that this script doesn't loop. +var compiling_var = 'compiling:' + process.env.npm_package_name; + +// When installing directly from GitHub, we get the TypeScript source, but none +// of the compiled JavaScript. +function isCompiled() { + // It will be. + if (process.env[compiling_var]) return true; + + try { + return !!require.resolve('.'); + } catch (error) { + return false; + } +} + +function isDirectory() { + var fullPath = path.join.apply(path, arguments); + try { + return fs.statSync(fullPath).isDirectory(); + } catch (error) { + return false; + } +} + +// When installing directly from GitHub, we get the source, but none of the +// compiled JavaScript. +if (isCompiled()) return; + +// Install our dev dependencies (aka TypeScript) into a sandbox, so that we can +// compile our code. +if (isDirectory(node_modules)) { + fs.renameSync(node_modules, temp_modules); +} + +var runner = /yarn/i.test(process.env.npm_config_user_agent) ? 'yarn' : 'npm'; + +process.env[compiling_var] = 'true'; +process.stdout.write('Compiling TypeScript sources… '); +childProcess.execSync(runner + ' install'); +childProcess.execSync(runner + ' run compile'); +process.stdout.write('done\n'); + +// Sneaky, but rimraf is included as a devDependency. +require('rimraf').sync(node_modules); + +// Restore any production dependencies. +if (isDirectory(temp_modules)) { + fs.renameSync(temp_modules, node_modules); +} diff --git a/scripts/postinstall.sh b/scripts/postinstall.sh deleted file mode 100755 index 9cb3180f..00000000 --- a/scripts/postinstall.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env bash -set -e - -source ./scripts/include/node.sh - -# When installing directly from GitHub, we get the source, but none of the -# compiled JavaScript. -if [[ -d ./node_modules/typescript ]]; then exit 0; fi - -# Install our dev dependencies (aka TypeScript) into a sandbox, so that we can -# compile our code. -if [[ -d ./node_modules ]]; then - mv ./node_modules ./node_modules.temp -fi - -echo "Compiling TypeScript sources… " -${RUNNER} install -run compile - -rm -rf node_modules -if [[ -d ./node_modules.temp ]]; then - mv ./node_modules.temp ./node_modules -fi