Skip to content

Commit daba910

Browse files
authored
Merge pull request #4 from sndrs/env
Update env
2 parents 3f32032 + 8538eed commit daba910

File tree

7 files changed

+97
-47
lines changed

7 files changed

+97
-47
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
node_modules
44
dist
5+
*.log

README.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
# ok
1+
# ok
22

3-
> 🙇‍♂️ Obliging task runner
3+
> 🙇‍♂️ An obliging task runner
44
5-
Automatically run a project’s `package.json` scripts in the right environment with no additional installation or config.
5+
Run `npm-scripts` in the correct environment without further installation or config.
66

77
<img src="demo.gif" width="444">
88

9-
## Install
9+
## Install
1010

1111
<a href="https://npmjs.org/package/@sndrs/ok" title="View this project on NPM"><img src="https://img.shields.io/npm/v/@sndrs/ok.svg" alt="NPM version" /></a>
1212

1313
```bash
1414
$ yarn global add @sndrs/ok
1515
```
16+
1617
or
18+
1719
```bash
1820
$ npm install -g @sndrs/ok
1921
```
@@ -25,7 +27,7 @@ $ npm install -g @sndrs/ok
2527
- [x] `engines`
2628
- [x] use project package manager (`yarn` or `npm`)
2729
- [x] use the checked in yarn version if present
28-
- [ ] use `engines` npm if present
30+
- [x] use `engines` npm if present
2931
- [x] always run tasks with up-to-date dependencies
3032

3133
### No dependencies

ok.code-workspace

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"folders": [
3+
{
4+
"name": "ok",
5+
"path": "."
6+
},
7+
{
8+
"name": "ok-test-yarn",
9+
"path": "/Users/alexsanders/Dev/ok-test-yarn"
10+
},
11+
{
12+
"name": "ok-test-npm",
13+
"path": "/Users/alexsanders/Dev/ok-test-npm"
14+
}
15+
],
16+
"settings": {}
17+
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@sndrs/ok",
33
"version": "0.1.2",
4-
"description": "Cli app that runs `package.json` scripts in the specified environment without configuring yours.",
4+
"description": "Run `npm-scripts` in the correct environment without further installation or config.",
55
"homepage": "https://github.com/sndrs/ok#readme",
66
"bugs": {
77
"url": "https://github.com/sndrs/ok/issues"

src/env.ts

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import execa from 'execa'
2+
import fs from 'fs'
3+
import path from 'path'
4+
import satisfies from 'semver/functions/satisfies'
5+
import validRange from 'semver/ranges/valid'
6+
import { error, info } from './log'
7+
8+
const cwd = process.cwd()
9+
const engines = require(path.resolve(cwd, 'package.json'))?.engines
10+
11+
let packageManager = fs.existsSync(path.resolve(cwd, 'yarn.lock'))
12+
? 'yarn'
13+
: fs.existsSync(path.resolve(cwd, 'package-lock.json'))
14+
? 'npm'
15+
: engines?.yarn
16+
? 'yarn'
17+
: engines?.npm
18+
? 'npm'
19+
: 'yarn'
20+
21+
export const getEnv = async () => {
22+
if (engines && engines[packageManager]) {
23+
const { stdout: packageManagerVersion } = await execa(
24+
packageManager,
25+
['-v'],
26+
{
27+
preferLocal: true,
28+
},
29+
)
30+
console.log(packageManagerVersion)
31+
32+
if (!satisfies(packageManagerVersion, engines[packageManager])) {
33+
if (packageManager === 'npm') {
34+
info('Installing npm')
35+
await execa('npm', [
36+
'install',
37+
'--no-save',
38+
'--silent',
39+
`npm@${engines[packageManager]}`,
40+
])
41+
} else {
42+
error(`Invalid yarn version:`)
43+
info(
44+
`Found ${packageManagerVersion} but package.json specifies ${engines[packageManager]} in \`engines.${packageManager}\`.`,
45+
)
46+
process.exit(1)
47+
}
48+
}
49+
}
50+
51+
const nvmrcPath = path.resolve(cwd, '.nvmrc')
52+
53+
const nvmrcNode = fs.existsSync(nvmrcPath)
54+
? validRange(fs.readFileSync(nvmrcPath, 'utf8').trim())
55+
: undefined
56+
57+
if (nvmrcNode === null) {
58+
error('Your .nvmrc version is not supported.')
59+
info('Please use a valid semver version range:')
60+
info('https://github.com/npm/node-semver#versions')
61+
process.exit(1)
62+
}
63+
64+
const nodeRange = [engines?.node, nvmrcNode, '*'].filter(Boolean).join(' ')
65+
66+
const useCurrentNode = satisfies(process.version, nodeRange)
67+
68+
return { nodeRange, useCurrentNode, packageManager }
69+
}

src/get-node-range.ts

-31
This file was deleted.

src/run.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import chalk from 'chalk'
22
import execa from 'execa'
3-
import fs from 'fs'
43
import nvexeca from 'nvexeca'
5-
import path from 'path'
64
import coerce from 'semver/functions/coerce'
7-
import satisfies from 'semver/functions/satisfies'
8-
import { getNodeRange } from './get-node-range'
5+
import { getEnv } from './env'
96
import { info } from './log'
107

118
const tick = chalk.green('✓')
@@ -14,12 +11,7 @@ const workingEnv = (node: string, pm: string) =>
1411
`${tick} Using ${pm} and Node v${coerce(node)}`
1512

1613
export const run = async (tasks: string[] = []) => {
17-
const nodeRange = getNodeRange()
18-
19-
const useCurrentNode = satisfies(process.version, nodeRange)
20-
21-
const yarnLockPath = path.resolve(process.cwd(), 'yarn.lock')
22-
const packageManager = fs.existsSync(yarnLockPath) ? 'yarn' : 'npm'
14+
const { nodeRange, packageManager, useCurrentNode } = await getEnv()
2315

2416
const _execa = (args: string[]) =>
2517
useCurrentNode

0 commit comments

Comments
 (0)