@atomicsmash/cli 12.0.0-beta.1 → 12.0.0-beta.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.
@@ -0,0 +1,4 @@
1
+ export declare const command = "add-media-proxy-to-herd";
2
+ export declare const describe = "Add the media proxy to the local NGINX config within Herd.";
3
+ export declare function handler(): Promise<void>;
4
+ //# sourceMappingURL=add-media-proxy-to-herd.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"add-media-proxy-to-herd.d.ts","sourceRoot":"","sources":["../../src/commands/add-media-proxy-to-herd.ts"],"names":[],"mappings":"AA6FA,eAAO,MAAM,OAAO,4BAA4B,CAAC;AACjD,eAAO,MAAM,QAAQ,+DACwC,CAAC;AAE9D,wBAAsB,OAAO,kBAiD5B"}
@@ -1,11 +1,10 @@
1
1
  import { exec } from "node:child_process";
2
- import { readFile, writeFile } from "node:fs/promises";
3
- import { homedir, type } from "node:os";
4
- import { join } from "node:path";
5
2
  import { promisify } from "node:util";
6
3
  import { getSmashConfig } from "@atomicsmash/smash-config";
4
+ import { homedir, type } from "node:os";
5
+ import { join } from "node:path";
6
+ import { readFile, writeFile } from "node:fs/promises";
7
7
  const PROXY_MARKER = "location @uploadsproxy";
8
- const execute = promisify(exec);
9
8
  function buildProxyBlock(stagingURL, httpAuth) {
10
9
  const authHeader = httpAuth
11
10
  ? `\n proxy_set_header Authorization "Basic ${httpAuth}";`
@@ -43,43 +42,52 @@ function addProxyBlock(config, stagingURL, httpAuth) {
43
42
  "\n" +
44
43
  config.slice(serverBlockEnd));
45
44
  }
46
- function removeProxyBlock(config) {
47
- return config.replace(/\n\s*location \^~ \/wp-content\/uploads\/\s*\{[\s\S]*?location @uploadsproxy\s*\{[\s\S]*?\}/, "");
48
- }
49
- export const command = "toggle-media-proxy";
50
- export const describe = "Toggle the media proxy in the local NGINX config within Herd.";
51
- export async function handler() {
52
- const smashConfig = await getSmashConfig(2);
53
- const { projectName, staging: { url: stagingURL, httpAuth: stagingHttpAuth }, } = smashConfig;
45
+ function getNginxConfigPath(projectName) {
54
46
  const isMacOS = type() === "Darwin";
55
- const nginxConfigPath = join(homedir(), isMacOS
47
+ return join(homedir(), isMacOS
56
48
  ? "Library/Application Support/Herd/config/valet/Nginx"
57
49
  : ".config\\herd\\config\\nginx", `${projectName}.test`);
58
- let config;
50
+ }
51
+ async function getNginxConfig(projectName) {
52
+ const nginxConfigPath = getNginxConfigPath(projectName);
59
53
  try {
60
- config = await readFile(nginxConfigPath, "utf-8");
54
+ return await readFile(nginxConfigPath, "utf-8");
61
55
  }
62
56
  catch {
63
57
  throw new Error(`Could not read NGINX config at: ${nginxConfigPath}\nMake sure the site exists in Herd.`);
64
58
  }
59
+ }
60
+ async function updateNginxConfig(projectName, updatedConfig) {
61
+ const nginxConfigPath = getNginxConfigPath(projectName);
62
+ await writeFile(nginxConfigPath, updatedConfig, "utf-8");
63
+ }
64
+ const execute = promisify(exec);
65
+ export const command = "add-media-proxy-to-herd";
66
+ export const describe = "Add the media proxy to the local NGINX config within Herd.";
67
+ export async function handler() {
68
+ const smashConfig = await getSmashConfig(2);
69
+ const { projectName, staging: { url: stagingURL, httpAuth: stagingHttpAuth }, } = smashConfig;
70
+ const nginxConfig = await getNginxConfig(projectName);
71
+ if (nginxConfig.includes(PROXY_MARKER)) {
72
+ console.log("Media proxy is already set up in your NGINX config. To remove please delete the site in Herd, remove your .env file and re-run setup.");
73
+ return;
74
+ }
65
75
  const httpAuth = stagingHttpAuth
66
76
  ? Buffer.from(`${stagingHttpAuth.username}:${stagingHttpAuth.password}`).toString("base64")
67
77
  : undefined;
68
- let updatedConfig;
69
- if (config.includes(PROXY_MARKER)) {
70
- updatedConfig = removeProxyBlock(config);
71
- console.log("Media proxy removed from NGINX config.");
72
- }
73
- else {
74
- updatedConfig = addProxyBlock(config, stagingURL, httpAuth);
78
+ const updatedConfig = addProxyBlock(nginxConfig, stagingURL, httpAuth);
79
+ await updateNginxConfig(projectName, updatedConfig)
80
+ .then(() => {
75
81
  if (httpAuth) {
76
82
  console.log("Media proxy added to NGINX config (with HTTP auth).");
77
83
  }
78
84
  else {
79
- console.log("Media proxy added to NGINX config (no HTTP auth — STAGING_HTTP_AUTH_USERNAME and STAGING_HTTP_AUTH_PASSWORD not set).");
85
+ console.log("Media proxy added to NGINX config (no HTTP auth — httpAuth not set in smash.config.ts).");
80
86
  }
81
- }
82
- await writeFile(nginxConfigPath, updatedConfig, "utf-8");
87
+ })
88
+ .catch(() => {
89
+ console.log("Failed to update your NGINX config. Please try running the command again.");
90
+ });
83
91
  console.log("Restarting Herd...");
84
92
  await execute("herd restart")
85
93
  .then(() => {
@@ -1 +1 @@
1
- {"version":3,"file":"pull-database.d.ts","sourceRoot":"","sources":["../../src/commands/pull-database.ts"],"names":[],"mappings":"AAOA,eAAO,MAAM,OAAO,kBAAkB,CAAC;AACvC,eAAO,MAAM,QAAQ,oEAC6C,CAAC;AAEnE,wBAAsB,OAAO,kBA4I5B"}
1
+ {"version":3,"file":"pull-database.d.ts","sourceRoot":"","sources":["../../src/commands/pull-database.ts"],"names":[],"mappings":"AAOA,eAAO,MAAM,OAAO,kBAAkB,CAAC;AACvC,eAAO,MAAM,QAAQ,oEAC6C,CAAC;AAEnE,wBAAsB,OAAO,kBAgJ5B"}
@@ -9,7 +9,7 @@ export const describe = "Pull the database down from staging and replace local d
9
9
  export async function handler() {
10
10
  const execute = promisify(exec);
11
11
  const smashConfig = await getSmashConfig(2);
12
- const { projectName, staging: { url: stagingURL, dbPrefix: stagingDBPrefix, webRoot: stagingWebRoot, ssh: { username: stagingSSHUsername, host: stagingSSHHost, port: stagingSSHPort, }, }, } = smashConfig;
12
+ const { projectName, staging: { url: stagingURL, dbPrefix: stagingDBPrefix, webRoot: stagingWebRoot, ssh: { username: stagingSSHUsername, host: stagingSSHHost, port: stagingSSHPort, }, }, cli: { pullMedia: { monthsToPull }, }, } = smashConfig;
13
13
  const stopRunningMessage = startRunningMessage("Pulling database from staging");
14
14
  performance.mark("Start");
15
15
  await (async () => {
@@ -92,7 +92,8 @@ export async function handler() {
92
92
  await stopRunningMessage4();
93
93
  });
94
94
  console.log(`Database import complete! ${convertMeasureToPrettyString(performance.measure("everything", "Start"))}`);
95
- console.log("If you want to download recent media you can use `npm run pull:media`, you can also change the number of months to download in your `.env` file.");
95
+ console.log(`If you're using Herd, you can now run the proxy-media command to avoid having to download images.
96
+ Otherwise, you can use pull:media for a slow download of ${monthsToPull === -1 ? "all the images" : `${monthsToPull.toString()} months worth of images`} from staging.`);
96
97
  })
97
98
  .catch(async (error) => {
98
99
  await stopRunningMessage();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atomicsmash/cli",
3
- "version": "12.0.0-beta.1",
3
+ "version": "12.0.0-beta.2",
4
4
  "description": "A collection of CLI tools by Atomic Smash.",
5
5
  "keywords": [],
6
6
  "homepage": "https://www.atomicsmash.co.uk/",
@@ -1,4 +0,0 @@
1
- export declare const command = "toggle-media-proxy";
2
- export declare const describe = "Toggle the media proxy in the local NGINX config within Herd.";
3
- export declare function handler(): Promise<void>;
4
- //# sourceMappingURL=toggle-media-proxy.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"toggle-media-proxy.d.ts","sourceRoot":"","sources":["../../src/commands/toggle-media-proxy.ts"],"names":[],"mappings":"AAuEA,eAAO,MAAM,OAAO,uBAAuB,CAAC;AAC5C,eAAO,MAAM,QAAQ,kEAC2C,CAAC;AAEjE,wBAAsB,OAAO,kBA2D5B"}