@curl.md/amp 0.0.1 → 0.0.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
@@ -5,9 +5,12 @@ Amp plugin for `curl.md`.
5
5
  ## Install
6
6
 
7
7
  ```sh
8
- pnpm dlx @curl.md/amp install
8
+ npx @curl.md/amp install
9
9
  ```
10
10
 
11
+ Successful installs print the Amp config path and create
12
+ `~/.config/amp/plugins/curlmd.ts`.
13
+
11
14
  ## License
12
15
 
13
16
  [MIT](https://github.com/wevm/curl.md/blob/main/LICENSE)
package/dist/install.js CHANGED
@@ -1,53 +1,62 @@
1
1
  #!/usr/bin/env node
2
2
  import { spawnSync } from 'node:child_process';
3
+ import { realpathSync } from 'node:fs';
3
4
  import fs from 'node:fs/promises';
4
5
  import os from 'node:os';
5
6
  import path from 'node:path';
6
7
  import process from 'node:process';
7
8
  import { fileURLToPath } from 'node:url';
8
- if (isMain())
9
- await main();
10
- export async function installAmpPlugin(options = {}) {
11
- const env = options.env || process.env;
12
- const platform = options.platform || process.platform;
13
- const packageJson = options.packageJson || (await readPackageJson());
14
- const ampConfigDir = options.ampConfigDir || getAmpConfigDir(env, platform);
15
- const packageSpec = `${packageJson.name}@${packageJson.version}`;
16
- await ensurePackageJson(ampConfigDir);
17
- installPackage(ampConfigDir, packageSpec, options.spawnSync || spawnSync, env);
18
- const shimPath = await writePluginShim(ampConfigDir);
19
- return { ampConfigDir, packageSpec, shimPath };
20
- }
21
- export function getAmpConfigDir(env = process.env, platform = process.platform) {
22
- if (env.AMP_CONFIG_DIR)
23
- return env.AMP_CONFIG_DIR;
24
- if (platform === 'win32') {
25
- const appData = env.APPDATA || path.join(os.homedir(), 'AppData', 'Roaming');
26
- return path.join(appData, 'amp');
27
- }
28
- const configHome = env.XDG_CONFIG_HOME || path.join(os.homedir(), '.config');
29
- return path.join(configHome, 'amp');
30
- }
31
- async function main() {
9
+ const isMain = (() => {
10
+ if (!process.argv[1])
11
+ return false;
12
+ const entryPath = (() => {
13
+ try {
14
+ return realpathSync(process.argv[1]);
15
+ }
16
+ catch {
17
+ return path.resolve(process.argv[1]);
18
+ }
19
+ })();
20
+ const modulePath = (() => {
21
+ try {
22
+ return realpathSync(fileURLToPath(import.meta.url));
23
+ }
24
+ catch {
25
+ return path.resolve(fileURLToPath(import.meta.url));
26
+ }
27
+ })();
28
+ return entryPath === modulePath;
29
+ })();
30
+ if (isMain) {
32
31
  const command = process.argv[2];
33
32
  if (command && command !== 'install') {
34
- console.error('Usage: pnpm dlx @curl.md/amp install');
33
+ console.error('Usage: curlmd-amp install');
35
34
  process.exitCode = 1;
36
- return;
37
- }
38
- try {
39
- const result = await installAmpPlugin();
40
- console.log(`Installed ${result.packageSpec}`);
41
- console.log(`Amp config: ${result.ampConfigDir}`);
42
- console.log(`Plugin shim: ${result.shimPath}`);
43
- console.log('Next: run `amp`. If auth is needed, set `CURLMD_API_KEY` or run `curl.md auth login`.');
44
35
  }
45
- catch (error) {
46
- console.error(error instanceof Error ? error.message : String(error));
47
- process.exitCode = 1;
36
+ else {
37
+ try {
38
+ const result = await installAmpPlugin();
39
+ console.log(`Installed ${result.packageSpec} to ${result.ampConfigDir}`);
40
+ console.log(`Plugin shim: ${result.shimPath}`);
41
+ console.log("Run 'amp' to get started");
42
+ console.log('If auth is needed, set `CURLMD_API_KEY` or run `curl.md auth login`.');
43
+ }
44
+ catch (error) {
45
+ console.error(`error: ${error instanceof Error ? error.message : String(error)}`);
46
+ process.exitCode = 1;
47
+ }
48
48
  }
49
49
  }
50
- async function ensurePackageJson(ampConfigDir) {
50
+ export async function installAmpPlugin(options = {}) {
51
+ const env = options.env || process.env;
52
+ const platform = options.platform || process.platform;
53
+ const ampConfigDir = options.ampConfigDir || getAmpConfigDir(env, platform);
54
+ // Resolve the published package/version we should install into Amp's config dir.
55
+ const packageJson = options.packageJson ||
56
+ JSON.parse(await fs.readFile(path.join(path.dirname(fileURLToPath(import.meta.url)), path.basename(path.dirname(fileURLToPath(import.meta.url))) === 'dist' ? '..' : '.', 'package.json'), 'utf8'));
57
+ const packageSpec = `${packageJson.name}@${packageJson.version}`;
58
+ console.log(`Preparing Amp config in ${ampConfigDir}`);
59
+ // Amp expects a standalone package root it can resolve plugins from.
51
60
  await fs.mkdir(ampConfigDir, { recursive: true });
52
61
  const packageJsonPath = path.join(ampConfigDir, 'package.json');
53
62
  try {
@@ -56,28 +65,21 @@ async function ensurePackageJson(ampConfigDir) {
56
65
  catch {
57
66
  await fs.writeFile(packageJsonPath, `${JSON.stringify({ name: 'amp-plugins', private: true }, undefined, 2)}\n`, 'utf8');
58
67
  }
59
- }
60
- function installPackage(ampConfigDir, packageSpec, spawn, env) {
61
- const result = spawn('pnpm', ['add', '--save-exact', packageSpec], {
68
+ console.log(`Installing ${packageSpec}`);
69
+ // Install the plugin package into that config-local node_modules.
70
+ const result = (options.spawnSync || spawnSync)(platform === 'win32' ? 'npm.cmd' : 'npm', ['install', '--save-exact', packageSpec], {
62
71
  cwd: ampConfigDir,
63
72
  env,
64
73
  stdio: 'inherit',
65
74
  });
66
75
  if (result.error)
67
76
  throw result.error;
68
- if (result.status === 0)
69
- return;
70
- throw new Error(`Failed to install ${packageSpec} into ${ampConfigDir}.`);
71
- }
72
- async function readPackageJson() {
73
- const currentDir = path.dirname(fileURLToPath(import.meta.url));
74
- const packageJsonPath = path.join(currentDir, path.basename(currentDir) === 'dist' ? '..' : '.', 'package.json');
75
- return JSON.parse(await fs.readFile(packageJsonPath, 'utf8'));
76
- }
77
- async function writePluginShim(ampConfigDir) {
78
- const pluginsDir = path.join(ampConfigDir, 'plugins');
79
- const shimPath = path.join(pluginsDir, 'curlmd.ts');
80
- await fs.mkdir(pluginsDir, { recursive: true });
77
+ if (result.status !== 0)
78
+ throw new Error(`Failed to install ${packageSpec} into ${ampConfigDir}.`);
79
+ // Register the plugin by writing the shim file Amp loads from ~/.config/amp/plugins.
80
+ const shimPath = path.join(ampConfigDir, 'plugins', 'curlmd.ts');
81
+ console.log(`Writing plugin shim to ${shimPath}`);
82
+ await fs.mkdir(path.dirname(shimPath), { recursive: true });
81
83
  await fs.writeFile(shimPath, [
82
84
  '// @i-know-the-amp-plugin-api-is-wip-and-very-experimental-right-now',
83
85
  "import plugin from '@curl.md/amp'",
@@ -85,10 +87,15 @@ async function writePluginShim(ampConfigDir) {
85
87
  'export default plugin',
86
88
  '',
87
89
  ].join('\n'), 'utf8');
88
- return shimPath;
90
+ return { ampConfigDir, packageSpec, shimPath };
89
91
  }
90
- function isMain() {
91
- if (!process.argv[1])
92
- return false;
93
- return path.resolve(process.argv[1]) === fileURLToPath(import.meta.url);
92
+ export function getAmpConfigDir(env = process.env, platform = process.platform) {
93
+ if (env.AMP_CONFIG_DIR)
94
+ return env.AMP_CONFIG_DIR;
95
+ if (platform === 'win32') {
96
+ const appData = env.APPDATA || path.join(os.homedir(), 'AppData', 'Roaming');
97
+ return path.join(appData, 'amp');
98
+ }
99
+ const configHome = env.XDG_CONFIG_HOME || path.join(os.homedir(), '.config');
100
+ return path.join(configHome, 'amp');
94
101
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@curl.md/amp",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "curl.md plugin for Amp",
5
5
  "bin": {
6
6
  "curlmd-amp": "./dist/install.js"