|
1 | 1 | #!/usr/bin/env node
|
2 | 2 | // @ts-check
|
3 |
| -import { spawnSync } from "node:child_process"; |
4 |
| -import * as fs from "node:fs"; |
5 |
| -import * as https from "node:https"; |
6 | 3 | import { createRequire } from "node:module";
|
7 |
| -import * as os from "node:os"; |
8 | 4 | import * as path from "node:path";
|
9 | 5 | import { fileURLToPath } from "node:url";
|
10 | 6 | import prompts from "prompts";
|
11 | 7 | import * as colors from "./colors.mjs";
|
12 | 8 | import { configure, getDefaultPlatformPackageName } from "./configure.mjs";
|
13 |
| -import { |
14 |
| - fetchPackageMetadata, |
15 |
| - memo, |
16 |
| - readJSONFile, |
17 |
| - toVersionNumber, |
18 |
| - v, |
19 |
| -} from "./helpers.js"; |
| 9 | +import { memo, readJSONFile, toVersionNumber, v } from "./helpers.js"; |
20 | 10 | import { parseArgs } from "./parseargs.mjs";
|
21 |
| - |
22 |
| -/** |
23 |
| - * Invokes `tar xf`. |
24 |
| - * @param {string} archive |
25 |
| - */ |
26 |
| -function untar(archive) { |
27 |
| - const args = ["xf", archive]; |
28 |
| - const options = { cwd: path.dirname(archive) }; |
29 |
| - const result = spawnSync("tar", args, options); |
30 |
| - |
31 |
| - // If we run `tar` from Git Bash with a Windows path, it will fail with: |
32 |
| - // |
33 |
| - // tar: Cannot connect to C: resolve failed |
34 |
| - // |
35 |
| - // GNU Tar assumes archives with a colon in the file name are on another |
36 |
| - // machine. See also |
37 |
| - // https://www.gnu.org/software/tar/manual/html_section/file.html. |
38 |
| - if ( |
39 |
| - process.platform === "win32" && |
40 |
| - result.stderr.toString().includes("tar: Cannot connect to") |
41 |
| - ) { |
42 |
| - args.push("--force-local"); |
43 |
| - return spawnSync("tar", args, options); |
44 |
| - } |
45 |
| - |
46 |
| - return result; |
47 |
| -} |
48 |
| - |
49 |
| -/** |
50 |
| - * Fetches the tarball URL for the specified package and version. |
51 |
| - * @param {string} pkg |
52 |
| - * @param {string} version |
53 |
| - * @returns {Promise<string>} |
54 |
| - */ |
55 |
| -async function fetchPackageTarballURL(pkg, version) { |
56 |
| - const info = await fetchPackageMetadata(pkg); |
57 |
| - const specific = info.versions[version]; |
58 |
| - if (specific) { |
59 |
| - return specific.dist.tarball; |
60 |
| - } |
61 |
| - |
62 |
| - const versions = Object.keys(info.versions); |
63 |
| - for (let i = versions.length - 1; i >= 0; --i) { |
64 |
| - const v = versions[i]; |
65 |
| - if (v.startsWith(version)) { |
66 |
| - return info.versions[v].dist.tarball; |
67 |
| - } |
68 |
| - } |
69 |
| - |
70 |
| - throw new Error(`No match found for '${pkg}@${version}'`); |
71 |
| -} |
| 11 | +import { downloadPackage, fetchPackageMetadata } from "./utils/npm.mjs"; |
72 | 12 |
|
73 | 13 | /**
|
74 | 14 | * Returns the installed `react-native` manifest, if present.
|
@@ -165,35 +105,6 @@ async function getVersion(platforms) {
|
165 | 105 | return target;
|
166 | 106 | }
|
167 | 107 |
|
168 |
| -/** |
169 |
| - * Downloads the specified npm package. |
170 |
| - * @param {string} pkg |
171 |
| - * @param {string} version |
172 |
| - * @returns {Promise<string>} |
173 |
| - */ |
174 |
| -async function downloadPackage(pkg, version) { |
175 |
| - const url = await fetchPackageTarballURL(pkg, version); |
176 |
| - console.log(`Downloading ${path.basename(url)}...`); |
177 |
| - |
178 |
| - return new Promise((resolve, reject) => { |
179 |
| - https |
180 |
| - .get(url, (res) => { |
181 |
| - const tmpDir = path.join(os.tmpdir(), "react-native-test-app"); |
182 |
| - fs.mkdirSync(tmpDir, { recursive: true }); |
183 |
| - |
184 |
| - const dest = path.join(tmpDir, path.basename(url)); |
185 |
| - const fh = fs.createWriteStream(dest); |
186 |
| - res.pipe(fh); |
187 |
| - fh.on("finish", () => { |
188 |
| - fh.close(); |
189 |
| - untar(dest); |
190 |
| - resolve(path.join(tmpDir, "package")); |
191 |
| - }); |
192 |
| - }) |
193 |
| - .on("error", (err) => reject(err)); |
194 |
| - }); |
195 |
| -} |
196 |
| - |
197 | 108 | /**
|
198 | 109 | * Returns the React Native version and path to the template.
|
199 | 110 | * @param {import("./types.js").Platform[]} platforms
|
|
0 commit comments