create-amytis 0.1.1 → 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.
Files changed (2) hide show
  1. package/dist/index.js +54 -18
  2. package/package.json +3 -7
package/dist/index.js CHANGED
@@ -7,7 +7,7 @@ import * as fs from "fs";
7
7
  import * as path from "path";
8
8
  import * as https from "https";
9
9
  import * as readline from "readline";
10
- import { execSync } from "child_process";
10
+ import { execFileSync, execSync } from "child_process";
11
11
  function prompt(question, defaultVal = "") {
12
12
  const rl = readline.createInterface({
13
13
  input: process.stdin,
@@ -22,20 +22,12 @@ function prompt(question, defaultVal = "") {
22
22
  });
23
23
  }
24
24
  function fetchJson(url) {
25
- const headers = { "User-Agent": "create-amytis-cli" };
26
- if (process.env.GITHUB_TOKEN) {
27
- headers["Authorization"] = `Bearer ${process.env.GITHUB_TOKEN}`;
28
- }
29
25
  return new Promise((resolve2, reject) => {
30
- https.get(url, { headers }, (res) => {
26
+ https.get(url, { headers: { "User-Agent": "create-amytis-cli" } }, (res) => {
31
27
  if (res.statusCode === 301 || res.statusCode === 302) {
32
28
  fetchJson(res.headers.location).then(resolve2).catch(reject);
33
29
  return;
34
30
  }
35
- if (res.statusCode === 403) {
36
- reject(new Error("GitHub API rate limit exceeded. Set GITHUB_TOKEN env var to increase the limit."));
37
- return;
38
- }
39
31
  if (res.statusCode !== 200) {
40
32
  reject(new Error(`GitHub API returned ${res.statusCode} for ${url}`));
41
33
  return;
@@ -76,10 +68,52 @@ function downloadFile(url, dest) {
76
68
  doGet(url);
77
69
  });
78
70
  }
79
- function extractTarball(tarPath, outDir) {
71
+ function getArchiveMetadata(tag, platform = process.platform) {
72
+ if (platform === "win32") {
73
+ return {
74
+ url: `https://github.com/hutusi/amytis/archive/refs/tags/${tag}.zip`,
75
+ filename: `amytis-${tag}.zip`,
76
+ kind: "zip"
77
+ };
78
+ }
79
+ return {
80
+ url: `https://github.com/hutusi/amytis/archive/refs/tags/${tag}.tar.gz`,
81
+ filename: `amytis-${tag}.tar.gz`,
82
+ kind: "tar.gz"
83
+ };
84
+ }
85
+ function buildExtractCommand(archivePath, tmpDir, kind, platform = process.platform) {
86
+ if (kind === "zip") {
87
+ if (platform !== "win32") {
88
+ throw new Error("ZIP extraction is only supported on Windows in create-amytis");
89
+ }
90
+ return {
91
+ command: "powershell.exe",
92
+ args: [
93
+ "-NoProfile",
94
+ "-NonInteractive",
95
+ "-ExecutionPolicy",
96
+ "Bypass",
97
+ "-Command",
98
+ "Expand-Archive",
99
+ "-LiteralPath",
100
+ archivePath,
101
+ "-DestinationPath",
102
+ tmpDir,
103
+ "-Force"
104
+ ]
105
+ };
106
+ }
107
+ return {
108
+ command: "tar",
109
+ args: ["xzf", archivePath, "-C", tmpDir]
110
+ };
111
+ }
112
+ function extractArchive(archivePath, outDir, kind, platform = process.platform) {
80
113
  const tmpDir = `${outDir}.__tmp__`;
81
114
  fs.mkdirSync(tmpDir, { recursive: true });
82
- execSync(`tar xzf "${tarPath}" -C "${tmpDir}"`);
115
+ const { command, args } = buildExtractCommand(archivePath, tmpDir, kind, platform);
116
+ execFileSync(command, args, { stdio: "inherit" });
83
117
  const entries = fs.readdirSync(tmpDir);
84
118
  if (entries.length !== 1) {
85
119
  throw new Error(`Unexpected tarball structure: ${entries.join(", ")}`);
@@ -87,7 +121,7 @@ function extractTarball(tarPath, outDir) {
87
121
  const innerDir = path.join(tmpDir, entries[0]);
88
122
  fs.renameSync(innerDir, outDir);
89
123
  fs.rmdirSync(tmpDir);
90
- fs.unlinkSync(tarPath);
124
+ fs.unlinkSync(archivePath);
91
125
  }
92
126
  function escapeTemplateLiteral(str) {
93
127
  return str.replace(/\\/g, "\\\\").replace(/`/g, "\\`").replace(/\$\{/g, "\\${");
@@ -140,12 +174,12 @@ Fetching latest Amytis release...`);
140
174
  if (!tag)
141
175
  throw new Error("Could not determine latest release tag");
142
176
  console.log(` Found: ${tag}`);
143
- const tarUrl = `https://github.com/hutusi/amytis/archive/refs/tags/${tag}.tar.gz`;
144
- const tarDest = path.join(process.cwd(), `amytis-${tag}.tar.gz`);
177
+ const archive = getArchiveMetadata(tag);
178
+ const archiveDest = path.join(process.cwd(), archive.filename);
145
179
  console.log("Downloading tarball...");
146
- await downloadFile(tarUrl, tarDest);
180
+ await downloadFile(archive.url, archiveDest);
147
181
  console.log("Extracting...");
148
- extractTarball(tarDest, targetDir);
182
+ extractArchive(archiveDest, targetDir, archive.kind);
149
183
  console.log(` Scaffolded: ${targetDir}`);
150
184
  const siteTitle = await prompt(`
151
185
  Site title`, projectName);
@@ -177,5 +211,7 @@ Error:`, err.message);
177
211
  }
178
212
  export {
179
213
  patchSiteConfig,
180
- patchPackageJson
214
+ patchPackageJson,
215
+ getArchiveMetadata,
216
+ buildExtractCommand
181
217
  };
package/package.json CHANGED
@@ -1,17 +1,13 @@
1
1
  {
2
2
  "name": "create-amytis",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Create a new Amytis digital garden",
5
5
  "license": "MIT",
6
- "bin": {
7
- "create-amytis": "dist/index.js"
8
- },
6
+ "bin": { "create-amytis": "./dist/index.js" },
9
7
  "scripts": {
10
8
  "build": "bun build src/index.ts --outfile dist/index.js --target node --sourcemap=none",
11
9
  "dev": "bun src/index.ts",
12
10
  "test": "bun test src"
13
11
  },
14
- "files": [
15
- "dist"
16
- ]
12
+ "files": ["dist"]
17
13
  }