@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 +4 -1
- package/dist/install.js +65 -58
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,9 +5,12 @@ Amp plugin for `curl.md`.
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
7
7
|
```sh
|
|
8
|
-
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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:
|
|
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
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
|
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
|
-
|
|
61
|
-
const result =
|
|
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
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
|
91
|
-
if (
|
|
92
|
-
return
|
|
93
|
-
|
|
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
|
}
|