Skip to content

Commit b4449e2

Browse files
committed
feat(config): parse relative date in --before
1 parent 4a32606 commit b4449e2

File tree

8 files changed

+33
-2
lines changed

8 files changed

+33
-2
lines changed

DEPENDENCIES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@ graph LR;
579579
npmcli-arborist-->walk-up-path;
580580
npmcli-config-->ci-info;
581581
npmcli-config-->ini;
582+
npmcli-config-->ms;
582583
npmcli-config-->nopt;
583584
npmcli-config-->npmcli-eslint-config["@npmcli/eslint-config"];
584585
npmcli-config-->npmcli-map-workspaces["@npmcli/map-workspaces"];

package-lock.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8365,6 +8365,8 @@
83658365
},
83668366
"node_modules/ms": {
83678367
"version": "2.1.3",
8368+
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
8369+
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==",
83688370
"inBundle": true,
83698371
"license": "MIT"
83708372
},
@@ -14736,6 +14738,7 @@
1473614738
"@npmcli/package-json": "^7.0.0",
1473714739
"ci-info": "^4.0.0",
1473814740
"ini": "^6.0.0",
14741+
"ms": "^2.1.3",
1473914742
"nopt": "^9.0.0",
1474014743
"proc-log": "^6.0.0",
1474114744
"semver": "^7.3.5",

tap-snapshots/test/lib/docs.js.test.cjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,15 @@ config is given, this value will always be set to \`legacy\`.
260260
#### \`before\`
261261
262262
* Default: null
263-
* Type: null or Date
263+
* Type: null, Date, or class RelativeDate {}
264264
265265
If passed to \`npm install\`, will rebuild the npm tree such that only
266266
versions that were available **on or before** the given date are installed.
267267
If there are no versions available for the current set of dependencies, the
268268
command will error.
269269
270+
Accepts either a Date string or a relative date, e.g. "24h", "7d".
271+
270272
If the requested version is a \`dist-tag\` and the given tag does not pass the
271273
\`--before\` filter, the most recent version less than or equal to that tag
272274
will be used. For example, \`foo@latest\` might install \`foo@1.2\` even though

workspaces/config/lib/definitions/definitions.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ const {
9292
Umask: { type: Umask },
9393
url: { type: url },
9494
path: { type: path },
95+
relativeDate: { type: RelativeDate },
9596
} = require('../type-defs.js')
9697

9798
// basic flattening function, just copy it over camelCase
@@ -231,13 +232,15 @@ const definitions = {
231232
before: new Definition('before', {
232233
default: null,
233234
hint: '<date>',
234-
type: [null, Date],
235+
type: [null, Date, RelativeDate],
235236
description: `
236237
If passed to \`npm install\`, will rebuild the npm tree such that only
237238
versions that were available **on or before** the given date are
238239
installed. If there are no versions available for the current set of
239240
dependencies, the command will error.
240241
242+
Accepts either a Date string or a relative date, e.g. "24h", "7d".
243+
241244
If the requested version is a \`dist-tag\` and the given tag does not
242245
pass the \`--before\` filter, the most recent version less than or equal
243246
to that tag will be used. For example, \`foo@latest\` might install

workspaces/config/lib/type-defs.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
const nopt = require('nopt')
2+
const ms = require('ms')
23

34
const { validate: validateUmask } = require('./umask.js')
45

56
class Umask {}
67
class Semver {}
8+
class RelativeDate {}
79
const semverValid = require('semver/functions/valid')
810
const validateSemver = (data, k, val) => {
911
const valid = semverValid(val)
@@ -21,6 +23,14 @@ const validatePath = (data, k, val) => {
2123
return noptValidatePath(data, k, val)
2224
}
2325

26+
const validateRelativeDate = (data, k, val) => {
27+
const valid = ms(val)
28+
if (valid === undefined) {
29+
return false
30+
}
31+
data[k] = new Date(Date.now() - valid)
32+
}
33+
2434
// add descriptions so we can validate more usefully
2535
module.exports = {
2636
...nopt.typeDefs,
@@ -34,6 +44,11 @@ module.exports = {
3444
validate: validateUmask,
3545
description: 'octal number in range 0o000..0o777 (0..511)',
3646
},
47+
relativeDate: {
48+
type: RelativeDate,
49+
validate: validateRelativeDate,
50+
description: 'valid relative date string e.g. "24h", "7d"',
51+
},
3752
url: {
3853
...nopt.typeDefs.url,
3954
description: 'full url with "http://"',

workspaces/config/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"@npmcli/package-json": "^7.0.0",
4242
"ci-info": "^4.0.0",
4343
"ini": "^6.0.0",
44+
"ms": "^2.1.3",
4445
"nopt": "^9.0.0",
4546
"proc-log": "^6.0.0",
4647
"semver": "^7.3.5",

workspaces/config/tap-snapshots/test/type-description.js.test.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Object {
4646
"before": Array [
4747
null,
4848
"valid Date string",
49+
"valid relative date string e.g. \\"24h\\", \\"7d\\"",
4950
],
5051
"bin-links": Array [
5152
"boolean value (true or false)",

workspaces/config/test/type-defs.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ const {
77
path: {
88
validate: validatePath,
99
},
10+
relativeDate: {
11+
validate: validateRelativeDate,
12+
},
1013
} = typeDefs
1114
const { resolve } = require('node:path')
1215

@@ -20,3 +23,5 @@ t.equal(validatePath(d, 'somePath', null), false)
2023
t.equal(validatePath(d, 'somePath', 1234), false)
2124
t.equal(validatePath(d, 'somePath', 'false'), true)
2225
t.equal(d.somePath, resolve('false'))
26+
t.equal(validateRelativeDate(d, 'someDate', 'foobar'), false)
27+
t.equal(validateRelativeDate(d, 'someDate', '1d'), undefined)

0 commit comments

Comments
 (0)