@illumine/npm-diff 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # @illumine/npm-diff
2
+
3
+ Diff two versions of an npm package in VS Code, unminified and formatted.
4
+
5
+ ## CLI
6
+
7
+ ```sh
8
+ npx @illumine/npm-diff react 18.2.0 18.3.0
9
+ ```
10
+
11
+ Omit the versions to pick them interactively. Opens VS Code with the
12
+ [compare-folders](https://marketplace.visualstudio.com/items?itemName=moshfeu.compare-folders)
13
+ extension, so a `code` CLI on your `PATH` is required.
14
+
15
+ ## Options
16
+
17
+ | Option | Description | Default |
18
+ | ----------------- | -------------------------------------| ----------------------------- |
19
+ | `-r, --registry` | npm registry to query. | `https://registry.npmjs.org` |
20
+ | `-w, --workspace` | Directory to place the two versions. | a temp dir, removed on exit |
21
+ | `-u, --unminify` | Unminify sources before diffing. | off |
22
+
23
+ ## License
24
+
25
+ MIT
package/lib/index.cjs CHANGED
@@ -69,7 +69,7 @@ async function formatTree(dir) {
69
69
  try {
70
70
  const { code: formatted } = await oxfmt.format(path.basename(file), code, {
71
71
  tabWidth: 2,
72
- printWidth: 120
72
+ printWidth: 100
73
73
  });
74
74
  await promises.writeFile(file, formatted);
75
75
  } catch {
@@ -105,32 +105,39 @@ async function launchDiff(left, right) {
105
105
 
106
106
  // src/npm-diff.ts
107
107
  var NpmDiff = class {
108
- constructor(pkg, registry = DEFAULT_REGISTRY) {
108
+ constructor(pkg, options = {}) {
109
109
  this.pkg = pkg;
110
- this.registry = registry;
110
+ this.registry = options.registry ?? DEFAULT_REGISTRY;
111
+ this.workdir = options.workspace ? path.resolve(options.workspace) : zx.tmpdir("npmdiff-");
112
+ this.clean = options.workspace === void 0;
113
+ this.unminify = options.unminify ?? false;
111
114
  }
112
115
  pkg;
113
- registry;
114
116
  packument;
115
- workdir = zx.tmpdir("npmdiff-");
117
+ registry;
118
+ workdir;
119
+ clean;
120
+ unminify;
116
121
  async run(specA, specB) {
117
122
  await ensureVscode();
118
123
  this.packument = await zx.spinner(
119
124
  `Fetching '${this.pkg}' from npm\u2026`,
120
125
  () => fetchPackument(this.pkg, this.registry)
121
126
  );
122
- const [left, right] = specA && specB ? sortVersions(this.resolve(specA), this.resolve(specB)) : await this.pickVersions(specA);
123
- if (left === right) {
124
- throw new CliError(`Both specs resolve to ${left} \u2014 there is nothing to diff.`);
127
+ const [versionA, versionB] = specA && specB ? sortVersions(this.resolve(specA), this.resolve(specB)) : await this.pickVersions(specA);
128
+ if (versionA === versionB) {
129
+ throw new CliError(`Both specs resolve to ${versionA} \u2014 there is nothing to diff.`);
125
130
  }
126
131
  try {
127
- await this.prepare(left);
128
- await this.prepare(right);
129
- console.log(zx.chalk.green(`Opening VS Code to diff ${this.pkg} ${left} \u2194 ${right}`));
130
- await launchDiff(path.join(this.workdir, left), path.join(this.workdir, right));
131
- console.log(zx.chalk.dim("VS Code closed \u2014 cleaning up temporary files."));
132
+ const left = await this.prepare(versionA);
133
+ const right = await this.prepare(versionB);
134
+ console.log(zx.chalk.green(`Opening VS Code to diff ${this.pkg} ${versionA} \u2194 ${versionB}`));
135
+ await launchDiff(left, right);
136
+ console.log(
137
+ this.clean ? zx.chalk.dim("VS Code closed \u2014 cleaning up temporary files.") : zx.chalk.dim(`VS Code closed \u2014 files kept in ${this.workdir}`)
138
+ );
132
139
  } finally {
133
- await zx.fs.remove(this.workdir);
140
+ if (this.clean) await zx.fs.remove(this.workdir);
134
141
  }
135
142
  }
136
143
  /** Resolve a version or dist-tag into a concrete published version. */
@@ -161,8 +168,9 @@ var NpmDiff = class {
161
168
  await zx.fs.mkdirp(dir);
162
169
  const tarball = await zx.spinner(`Downloading ${label}`, () => this.download(version, dir));
163
170
  await zx.spinner(`Unpacking ${label}`, () => this.unpack(tarball, dir));
164
- await zx.spinner(`Unminifying ${label}`, () => unminifyTree(dir));
171
+ if (this.unminify) await zx.spinner(`Unminifying ${label}`, () => unminifyTree(dir));
165
172
  await zx.spinner(`Formatting ${label}`, () => formatTree(dir));
173
+ return dir;
166
174
  }
167
175
  async download(version, dir) {
168
176
  const { stdout } = await zx.$`npm pack ${this.pkg}@${version} --registry ${this.registry} --pack-destination ${dir} --json`;
@@ -178,9 +186,10 @@ var NpmDiff = class {
178
186
  // src/index.ts
179
187
  zx.$.verbose = false;
180
188
  var program = new extraTypings.Command();
181
- program.name("npmdiff").description("Diff two versions of an npm package in VS Code, unminified and formatted").argument("<pkgname>", "npm package name (scoped names supported)").argument("[spec-a]", "a version or dist-tag").argument("[spec-b]", "a version or dist-tag").option("--registry <url>", "npm registry to query", DEFAULT_REGISTRY).action(
182
- (pkgname, specA, specB, options) => new NpmDiff(pkgname, options.registry).run(specA, specB)
183
- );
189
+ program.name("npmdiff").description("Diff two versions of an npm package in VS Code, unminified and formatted").argument("<pkgname>", "npm package name (scoped names supported)").argument("[spec-a]", "a version or dist-tag").argument("[spec-b]", "a version or dist-tag").option("-r, --registry <url>", "npm registry to query", DEFAULT_REGISTRY).option(
190
+ "-w, --workspace <dir>",
191
+ "directory to place the two versions; kept instead of auto-cleaned when set"
192
+ ).option("-u, --unminify", "unminify sources before diffing", false).action((pkgname, specA, specB, options) => new NpmDiff(pkgname, options).run(specA, specB));
184
193
  program.parseAsync().catch((error) => {
185
194
  if (error instanceof CliError) {
186
195
  console.error(zx.chalk.red(error.message));
package/lib/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import { Command } from '@commander-js/extra-typings';
3
3
  import { $, chalk, tmpdir, spinner, fs, which, glob } from 'zx';
4
- import { join, basename } from 'path';
4
+ import { resolve, join, basename } from 'path';
5
5
  import semver from 'semver';
6
6
  import { extract } from 'tar';
7
7
  import { confirm, search } from '@inquirer/prompts';
@@ -63,7 +63,7 @@ async function formatTree(dir) {
63
63
  try {
64
64
  const { code: formatted } = await format(basename(file), code, {
65
65
  tabWidth: 2,
66
- printWidth: 120
66
+ printWidth: 100
67
67
  });
68
68
  await writeFile(file, formatted);
69
69
  } catch {
@@ -99,32 +99,39 @@ async function launchDiff(left, right) {
99
99
 
100
100
  // src/npm-diff.ts
101
101
  var NpmDiff = class {
102
- constructor(pkg, registry = DEFAULT_REGISTRY) {
102
+ constructor(pkg, options = {}) {
103
103
  this.pkg = pkg;
104
- this.registry = registry;
104
+ this.registry = options.registry ?? DEFAULT_REGISTRY;
105
+ this.workdir = options.workspace ? resolve(options.workspace) : tmpdir("npmdiff-");
106
+ this.clean = options.workspace === void 0;
107
+ this.unminify = options.unminify ?? false;
105
108
  }
106
109
  pkg;
107
- registry;
108
110
  packument;
109
- workdir = tmpdir("npmdiff-");
111
+ registry;
112
+ workdir;
113
+ clean;
114
+ unminify;
110
115
  async run(specA, specB) {
111
116
  await ensureVscode();
112
117
  this.packument = await spinner(
113
118
  `Fetching '${this.pkg}' from npm\u2026`,
114
119
  () => fetchPackument(this.pkg, this.registry)
115
120
  );
116
- const [left, right] = specA && specB ? sortVersions(this.resolve(specA), this.resolve(specB)) : await this.pickVersions(specA);
117
- if (left === right) {
118
- throw new CliError(`Both specs resolve to ${left} \u2014 there is nothing to diff.`);
121
+ const [versionA, versionB] = specA && specB ? sortVersions(this.resolve(specA), this.resolve(specB)) : await this.pickVersions(specA);
122
+ if (versionA === versionB) {
123
+ throw new CliError(`Both specs resolve to ${versionA} \u2014 there is nothing to diff.`);
119
124
  }
120
125
  try {
121
- await this.prepare(left);
122
- await this.prepare(right);
123
- console.log(chalk.green(`Opening VS Code to diff ${this.pkg} ${left} \u2194 ${right}`));
124
- await launchDiff(join(this.workdir, left), join(this.workdir, right));
125
- console.log(chalk.dim("VS Code closed \u2014 cleaning up temporary files."));
126
+ const left = await this.prepare(versionA);
127
+ const right = await this.prepare(versionB);
128
+ console.log(chalk.green(`Opening VS Code to diff ${this.pkg} ${versionA} \u2194 ${versionB}`));
129
+ await launchDiff(left, right);
130
+ console.log(
131
+ this.clean ? chalk.dim("VS Code closed \u2014 cleaning up temporary files.") : chalk.dim(`VS Code closed \u2014 files kept in ${this.workdir}`)
132
+ );
126
133
  } finally {
127
- await fs.remove(this.workdir);
134
+ if (this.clean) await fs.remove(this.workdir);
128
135
  }
129
136
  }
130
137
  /** Resolve a version or dist-tag into a concrete published version. */
@@ -155,8 +162,9 @@ var NpmDiff = class {
155
162
  await fs.mkdirp(dir);
156
163
  const tarball = await spinner(`Downloading ${label}`, () => this.download(version, dir));
157
164
  await spinner(`Unpacking ${label}`, () => this.unpack(tarball, dir));
158
- await spinner(`Unminifying ${label}`, () => unminifyTree(dir));
165
+ if (this.unminify) await spinner(`Unminifying ${label}`, () => unminifyTree(dir));
159
166
  await spinner(`Formatting ${label}`, () => formatTree(dir));
167
+ return dir;
160
168
  }
161
169
  async download(version, dir) {
162
170
  const { stdout } = await $`npm pack ${this.pkg}@${version} --registry ${this.registry} --pack-destination ${dir} --json`;
@@ -172,9 +180,10 @@ var NpmDiff = class {
172
180
  // src/index.ts
173
181
  $.verbose = false;
174
182
  var program = new Command();
175
- program.name("npmdiff").description("Diff two versions of an npm package in VS Code, unminified and formatted").argument("<pkgname>", "npm package name (scoped names supported)").argument("[spec-a]", "a version or dist-tag").argument("[spec-b]", "a version or dist-tag").option("--registry <url>", "npm registry to query", DEFAULT_REGISTRY).action(
176
- (pkgname, specA, specB, options) => new NpmDiff(pkgname, options.registry).run(specA, specB)
177
- );
183
+ program.name("npmdiff").description("Diff two versions of an npm package in VS Code, unminified and formatted").argument("<pkgname>", "npm package name (scoped names supported)").argument("[spec-a]", "a version or dist-tag").argument("[spec-b]", "a version or dist-tag").option("-r, --registry <url>", "npm registry to query", DEFAULT_REGISTRY).option(
184
+ "-w, --workspace <dir>",
185
+ "directory to place the two versions; kept instead of auto-cleaned when set"
186
+ ).option("-u, --unminify", "unminify sources before diffing", false).action((pkgname, specA, specB, options) => new NpmDiff(pkgname, options).run(specA, specB));
178
187
  program.parseAsync().catch((error) => {
179
188
  if (error instanceof CliError) {
180
189
  console.error(chalk.red(error.message));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@illumine/npm-diff",
3
- "version": "0.0.1",
3
+ "version": "0.1.0",
4
4
  "description": "Diff two versions of an npm package in VS Code, unminified and formatted",
5
5
  "type": "module",
6
6
  "bin": {