Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Unix wildcard and multiple platforms #33

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Node.js CI

on:
push:
branches: [ main, add-unix-wildcard-and-multiple-platforms ]
pull_request:
branches: [ main, add-unix-wildcard-and-multiple-platforms ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: '14'

- name: Install dependencies
run: npm install

- name: Run tests
run: npm test
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@ or directly from the npm run script like this:
npm run foo -- arg1 arg2
```

You can use the "*" wildcard to match all Unix platforms:

```json
"cross-os": {
"chmodux": {
"win32": "",
"*": "chmod u+x main.js"
}
}
```

You can also use comma-separated values to match multiple platforms:

```json
"cross-os": {
"chmodux": {
"win32": "",
"darwin,freebsd,linux,sunos": "chmod u+x main.js"
}
}
```

## License

Expand Down
18 changes: 17 additions & 1 deletion source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,23 @@ const pipeline = new Promise<string>(resolve => {
}

try {
return Promise.resolve({ command: config[ property ][ script ][ platform ], params, script })
let command = config[ property ][ script ][ platform ]

if (!command && platform !== 'win32') {
command = config[ property ][ script ]['*']
}

if (!command) {
const platforms = Object.keys(config[ property ][ script ])
for (const p of platforms) {
if (p.split(',').includes(platform)) {
command = config[ property ][ script ][ p ]
break
}
}
}

return Promise.resolve({ command, params, script })
} catch (e) {
throw script
}
Expand Down
18 changes: 12 additions & 6 deletions test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
"first": {
"win32": "echo hello from win32",
"linux": "echo hello from linux",
"darwin": "echo hello from darwin"
"darwin": "echo hello from darwin",
"*": "echo hello from unix"
},
"first-with-params": {
"win32": "echo hello from win32, I have arguments:",
"linux": "echo hello from linux, I have arguments:",
"darwin": "echo hello from darwin, I have arguments:"
"darwin": "echo hello from darwin, I have arguments:",
"*": "echo hello from unix, I have arguments:"
},
"second": {},
"second-with-params": {},
Expand Down Expand Up @@ -37,24 +39,28 @@
"fifth": {
"win32": "echo hello from cross-os win32",
"linux": "echo hello from cross-os linux",
"darwin": "echo hello from cross-os darwin"
"darwin": "echo hello from cross-os darwin",
"*": "echo hello from cross-os unix"
},
"fifth-with-params": {
"win32": "echo hello from cross-os win32, I have arguments:",
"linux": "echo hello from cross-os linux, I have arguments:",
"darwin": "echo hello from cross-os darwin, I have arguments:"
"darwin": "echo hello from cross-os darwin, I have arguments:",
"*": "echo hello from cross-os unix, I have arguments:"
},
"sixth": "echo i should not been seen",
"sixth-with-params": "echo i should not been seen and niether the following arguments:",
"foo": {
"win32": "echo bar from win32",
"linux": "echo bar from linux",
"darwin": "echo bar from darwin"
"darwin": "echo bar from darwin",
"darwin,freebsd,linux,sunos": "echo bar from unix"
},
"foo-with-params": {
"win32": "echo bar from win32, I have arguments:",
"linux": "echo bar from linux, I have arguments:",
"darwin": "echo bar from darwin, I have arguments:"
"darwin": "echo bar from darwin, I have arguments:",
"darwin,freebsd,linux,sunos": "echo bar from unix, I have arguments:"
}
}
}
28 changes: 28 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,32 @@ describe('Loader', () => {
})
})

it('should run the correct script for wildcard "*" on Unix platforms', () => {
if (platform !== 'win32') {
const stdout = execSync(`node ${cross} first`)
expect(stdout.toString()).to.match(/hello from unix/)
}
})

it('should run the correct script for wildcard "*" on Unix platforms and pass parameters', () => {
if (platform !== 'win32') {
const stdout = execSync(`node ${cross} first-with-params -- First Second Third`)
expect(stdout.toString()).to.match(/hello from unix, I have arguments: First Second Third/)
}
})

it('should run the correct script for comma-separated values on Unix platforms', () => {
if (platform !== 'win32') {
const stdout = execSync(`node ${cross} foo`)
expect(stdout.toString()).to.match(/bar from unix/)
}
})

it('should run the correct script for comma-separated values on Unix platforms and pass parameters', () => {
if (platform !== 'win32') {
const stdout = execSync(`node ${cross} foo-with-params -- First Second Third`)
expect(stdout.toString()).to.match(/bar from unix, I have arguments: First Second Third/)
}
})

})
Loading