Skip to content

Commit

Permalink
Add Windows Support (#16)
Browse files Browse the repository at this point in the history
* 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 0b52783.

* 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 9cbc606.

Co-authored-by: AJ ONeal <[email protected]>
  • Loading branch information
janniks and AJ ONeal authored Dec 4, 2020
1 parent c00a524 commit a1398b1
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 38 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
17 changes: 9 additions & 8 deletions .github/workflows/node.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
.github
.editorconfig
.prettierrc
README.md
test

# Created by https://www.gitignore.io/api/node,macos
Expand Down
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
34 changes: 18 additions & 16 deletions postinstall.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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`);
}
27 changes: 14 additions & 13 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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}`);
Expand All @@ -37,4 +38,4 @@ function execute(command) {
}
});

console.log("\nTest passed.");
console.log('\nTest passed.');

0 comments on commit a1398b1

Please sign in to comment.