@fexd/toolchain 0.1.0 → 0.1.2

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 CHANGED
@@ -1,5 +1,7 @@
1
1
  # @fexd/toolchain
2
2
 
3
+ [![npm version](https://img.shields.io/npm/v/@fexd/toolchain.svg)](https://www.npmjs.com/package/@fexd/toolchain)
4
+
3
5
  `@fexd/toolchain` 是一个面向前端项目的工具链运行器,用于让项目脚本固定使用指定版本的 Node.js 和 pnpm。
4
6
 
5
7
  它适合这种场景:不同项目依赖不同的 Node.js / pnpm 版本,但开发者希望自己的全局 shell 环境保持不变。通过 `tc` 执行的命令只会在当前子进程中使用项目指定的工具链,不会切换或污染全局 `node` / `pnpm`。
@@ -60,6 +62,7 @@ tc-version-pnpm=9.15.9
60
62
  常用命令:
61
63
 
62
64
  ```bash
65
+ tc --version
63
66
  tc doctor
64
67
  tc node -v
65
68
  tc node scripts/build.js
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fexd/toolchain",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Run project scripts with a project-pinned Node.js and pnpm toolchain.",
5
5
  "repository": {
6
6
  "type": "git",
package/src/cli.cjs CHANGED
@@ -4,6 +4,7 @@ const { readToolchainConfig } = require('./config.cjs');
4
4
  const { getDefaultCacheRoot } = require('./cache.cjs');
5
5
  const { ensureNode, ensurePnpm } = require('./install.cjs');
6
6
  const { ensurePnpmShim, buildToolchainPath } = require('./shims.cjs');
7
+ const packageJson = require('../package.json');
7
8
 
8
9
  async function runCli(argv, dependencies) {
9
10
  const deps = dependencies || {};
@@ -16,6 +17,11 @@ async function runCli(argv, dependencies) {
16
17
  const env = Object.assign({}, deps.env || process.env);
17
18
  const cwd = deps.cwd || process.cwd();
18
19
 
20
+ if (command === '--version' || command === '-v' || command === 'version') {
21
+ stdout.write(packageJson.version + '\n');
22
+ return 0;
23
+ }
24
+
19
25
  if (!command || command === '--help' || command === '-h') {
20
26
  stdout.write(usage());
21
27
  return 0;
@@ -69,6 +75,7 @@ async function runCli(argv, dependencies) {
69
75
  });
70
76
 
71
77
  if (command === 'node') {
78
+ stderr.write(formatAppliedVersions({ nodeVersion: config.nodeVersion }) + '\n');
72
79
  return spawnAndReturn({
73
80
  spawnSync: deps.spawnSync || childProcess.spawnSync,
74
81
  command: node.executablePath,
@@ -93,6 +100,11 @@ async function runCli(argv, dependencies) {
93
100
  pnpmCliPath: pnpm.executablePath
94
101
  });
95
102
 
103
+ stderr.write(formatAppliedVersions({
104
+ nodeVersion: config.nodeVersion,
105
+ pnpmVersion: config.pnpmVersion
106
+ }) + '\n');
107
+
96
108
  return spawnAndReturn({
97
109
  spawnSync: deps.spawnSync || childProcess.spawnSync,
98
110
  command: node.executablePath,
@@ -146,6 +158,16 @@ function getPathDelimiter(platform) {
146
158
  return platform === 'win32' ? ';' : ':';
147
159
  }
148
160
 
161
+ function formatAppliedVersions(options) {
162
+ const parts = ['[tc] using', 'node@' + options.nodeVersion];
163
+
164
+ if (options.pnpmVersion) {
165
+ parts.push('pnpm@' + options.pnpmVersion);
166
+ }
167
+
168
+ return parts.join(' ');
169
+ }
170
+
149
171
  function formatError(error) {
150
172
  return error && error.message ? error.message : String(error);
151
173
  }
@@ -153,6 +175,7 @@ function formatError(error) {
153
175
  function usage() {
154
176
  return [
155
177
  'Usage:',
178
+ ' tc --version',
156
179
  ' tc doctor',
157
180
  ' tc node <args...>',
158
181
  ' tc pnpm <args...>',
@@ -166,5 +189,6 @@ function usage() {
166
189
 
167
190
  module.exports = {
168
191
  runCli,
169
- usage
192
+ usage,
193
+ formatAppliedVersions
170
194
  };
package/src/install.cjs CHANGED
@@ -110,10 +110,12 @@ function downloadFile(url, destination) {
110
110
  });
111
111
  }
112
112
 
113
- function extractArchive(archivePath, destination) {
113
+ function extractArchive(archivePath, destination, options) {
114
+ const opts = options || {};
114
115
  fs.mkdirSync(destination, { recursive: true });
115
116
 
116
- const result = childProcess.spawnSync('tar', ['-xf', archivePath, '-C', destination], {
117
+ const spawn = opts.spawnSync || childProcess.spawnSync;
118
+ const result = spawn(getTarCommand(opts), ['-xf', archivePath, '-C', destination], {
117
119
  stdio: 'inherit'
118
120
  });
119
121
 
@@ -126,9 +128,23 @@ function extractArchive(archivePath, destination) {
126
128
  }
127
129
  }
128
130
 
131
+ function getTarCommand(options) {
132
+ const opts = options || {};
133
+ const platform = opts.platform || process.platform;
134
+
135
+ if (platform !== 'win32') {
136
+ return 'tar';
137
+ }
138
+
139
+ const env = opts.env || process.env;
140
+ const systemRoot = env.SystemRoot || env.SYSTEMROOT || 'C:\\Windows';
141
+ return path.win32.join(systemRoot, 'System32', 'tar.exe');
142
+ }
143
+
129
144
  module.exports = {
130
145
  ensureNode,
131
146
  ensurePnpm,
132
147
  downloadFile,
133
- extractArchive
148
+ extractArchive,
149
+ getTarCommand
134
150
  };