From a1398b1a1af23a8c9b402f3eba8984464e669c5f Mon Sep 17 00:00:00 2001 From: Jannik <6362150+janniks@users.noreply.github.com> Date: Fri, 4 Dec 2020 14:33:04 +0100 Subject: [PATCH] Add Windows Support (#16) * bugfix Windows support - use `path.sep` rather than assuming `/` - throw errors, not strings - replaced strict-mode reserved word "package" with "pkg" * Update dotfiles * Prepare for windows * Fix test paths * Revert "Fix test paths" This reverts commit 0b527839444716c4956950c191d1977f8326e9c5. * Test without init * Fix tests with init * Fix shell mode in tests * Remove package init in tests * Revert "Remove package init in tests" This reverts commit 9cbc606f7f8a093fca22d590e0dda9f00bf7ae2e. Co-authored-by: AJ ONeal --- .editorconfig | 13 +++++++++++++ .github/workflows/node.yml | 17 +++++++++-------- .npmignore | 3 +++ .prettierrc | 3 +++ package.json | 2 +- postinstall.js | 34 ++++++++++++++++++---------------- test/test.js | 27 ++++++++++++++------------- 7 files changed, 61 insertions(+), 38 deletions(-) create mode 100644 .editorconfig create mode 100644 .prettierrc diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..435e3c7 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +# http://editorconfig.org +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 2 +indent_style = space +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false diff --git a/.github/workflows/node.yml b/.github/workflows/node.yml index eb495e4..b08d7fc 100644 --- a/.github/workflows/node.yml +++ b/.github/workflows/node.yml @@ -8,16 +8,17 @@ on: jobs: build: - runs-on: ubuntu-latest + runs-on: ${{ matrix.os }} strategy: matrix: - node-version: [4.x, 6.x, 8.x, 10.x, 12.x] + node-version: [4.x, 6.x, 8.x, 10.x, 12.x, 14.x] + os: [macos-latest, ubuntu-latest, windows-latest] steps: - - uses: actions/checkout@v2 - - name: Set up Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v1 - with: - node-version: ${{ matrix.node-version }} - - run: npm test + - uses: actions/checkout@v2 + - name: Set up Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + - run: npm test diff --git a/.npmignore b/.npmignore index 8f8298b..0526908 100644 --- a/.npmignore +++ b/.npmignore @@ -1,4 +1,7 @@ .github +.editorconfig +.prettierrc +README.md test # Created by https://www.gitignore.io/api/node,macos diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..544138b --- /dev/null +++ b/.prettierrc @@ -0,0 +1,3 @@ +{ + "singleQuote": true +} diff --git a/package.json b/package.json index b9998f6..f129b48 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "basetag", - "version": "1.0.0", + "version": "1.1.0", "description": "A better way to import NodeJS modules", "main": "postinstall.js", "scripts": { diff --git a/postinstall.js b/postinstall.js index 4c38512..eea39c2 100644 --- a/postinstall.js +++ b/postinstall.js @@ -1,15 +1,17 @@ -const fs = require("fs"); -const path = require("path"); +const fs = require('fs'); +const path = require('path'); -const package = require("./package"); +const pkg = require('./package'); // Helpers -const reset = "\x1b[0m"; -const yellow = "\x1b[33m"; -const blue = "\x1b[34m"; +const reset = '\x1b[0m'; +const yellow = '\x1b[33m'; +const blue = '\x1b[34m'; const log = (message) => console.log(`${blue}${message}${reset}`); +const modulesDir = 'node_modules'; + function fileExists(path) { try { fs.accessSync(path); @@ -20,29 +22,29 @@ function fileExists(path) { } // Main -log(`${package.name}@${package.version}`); +log(`${pkg.name}@${pkg.version}`); try { - const lIndex = __dirname.lastIndexOf("/node_modules/"); + const lIndex = __dirname.lastIndexOf(path.sep + modulesDir + path.sep); if (lIndex === -1) { - throw "- Could not find node_modules directory in __dirname"; + throw new Error('- Could not find node_modules directory in __dirname'); } const base = path.resolve(__dirname.slice(0, lIndex)); - const atLink = path.resolve(base, "node_modules/$"); + const baseLink = path.resolve(base, modulesDir, '$'); - if (fileExists(atLink)) { - if (base === fs.realpathSync(atLink)) { - log("- $ symlink already points to base\n"); + if (fileExists(baseLink)) { + if (base === fs.realpathSync(baseLink)) { + log('- $ symlink already points to base\n'); process.exit(); } - throw "- File already exists: node_modules/$"; + throw new Error(`- File already exists: ${baseLink}`); } - fs.symlinkSync(base, atLink, "junction"); + fs.symlinkSync(base, baseLink, 'junction'); log(`- Created $ symlink to ${base}\n`); } catch (error) { - console.warn(`${yellow}${error}\n- Not creating $ symlink${reset}\n`); + console.warn(`${yellow}${error.message}\n- Not creating $ symlink${reset}\n`); } diff --git a/test/test.js b/test/test.js index bac7872..7060a5e 100644 --- a/test/test.js +++ b/test/test.js @@ -1,22 +1,23 @@ -const resolve = require("path").resolve; -const spawnSync = require("child_process").spawnSync; +const path = require('path'); +const spawnSync = require('child_process').spawnSync; -const package = require("../package"); +const pkg = require('../package'); -const example = resolve(__dirname, "example"); +const exampleDir = path.resolve(__dirname, 'example'); +console.log(exampleDir); function execute(command) { - const args = command.split(" "); + const args = command.split(' '); const prog = args.shift(); - return spawnSync(prog, args, { cwd: example }); + return spawnSync(prog, args, { cwd: exampleDir, shell: true }); } [ - "rm -rf node_modules *.json *.tgz", - "npm init -y", - "npm pack ../..", - `npm install --save ${package.name}-${package.version}.tgz`, - "node index.js", + 'rm -rf node_modules *.json *.tgz', + 'npm init -y', + `npm pack ..${path.sep}..`, + `npm install --save ${pkg.name}-${pkg.version}.tgz`, + 'node index.js', ].forEach((command) => { try { const result = execute(command); @@ -26,7 +27,7 @@ function execute(command) { if (result.status != 0) { console.log(`stdout:\n${result.stdout}`); console.error(`stderr:\n${result.stderr}`); - throw "Bad status code"; + throw 'Bad status code'; } console.log(`✓ ${command}`); @@ -37,4 +38,4 @@ function execute(command) { } }); -console.log("\nTest passed."); +console.log('\nTest passed.');