-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathnpm.ts
196 lines (179 loc) · 5.75 KB
/
npm.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import * as Config from '@taqueria/protocol/Config';
import type { i18n } from '@taqueria/protocol/i18n';
import * as InstalledPlugin from '@taqueria/protocol/InstalledPlugin';
import * as LoadedConfig from '@taqueria/protocol/LoadedConfig';
import * as NonEmptyString from '@taqueria/protocol/NonEmptyString';
import * as SanitizedAbsPath from '@taqueria/protocol/SanitizedAbsPath';
import * as SanitizedArgs from '@taqueria/protocol/SanitizedArgs';
import * as TaqError from '@taqueria/protocol/TaqError';
import { chain, chainRej, FutureInstance as Future, map, mapRej, reject, resolve } from 'fluture';
import { pipe } from 'fun';
import initPlugins from './plugins.ts';
import { EnvVars } from './taqueria-types.ts';
import * as utils from './taqueria-utils/taqueria-utils.ts';
// Get utils
const { execText, readJsonFile, writeJsonFile, rm, isTaqError } = utils.inject({
stdout: Deno.stdout,
stderr: Deno.stderr,
});
// Alias
const exec = execText;
// This file contains logic for handling plugins distributed
// and installable using NPM
export type NpmPluginName = string & { __kind__: 'NpmPluginName' };
interface Manifest {
name: string;
}
export const getPluginName = (input: string): NpmPluginName => {
const endIndex = input.lastIndexOf('@');
const hasVersion = endIndex > 0;
const retval = hasVersion
? input.substring(0, endIndex).trim()
: input.trim();
return (retval as NpmPluginName);
};
export const requireNPM = (projectDir: SanitizedAbsPath.t, i18n: i18n) =>
pipe(
SanitizedAbsPath.make(`${projectDir}/package.json`),
chain(abspath => readJsonFile<Manifest>(abspath)),
mapRej(previous => {
const taqErr: TaqError.t = {
kind: 'E_NPM_INIT',
msg: i18n.__('npmInitRequired'),
context: projectDir,
previous,
};
return taqErr;
}),
);
export const getPluginPackageJson = (pluginNameOrPath: string, projectDir: SanitizedAbsPath.t) =>
pipe(
/^\//.test(pluginNameOrPath)
// If starts with a slash, we can assume that its a path
? SanitizedAbsPath.make(pluginNameOrPath)
// Otherwise, we assume that its a relative path
: SanitizedAbsPath.make(`${projectDir}/${pluginNameOrPath}`),
chain(pluginPath => SanitizedAbsPath.make(`${pluginPath}/package.json`)),
chain(readJsonFile),
chainRej(() =>
// Assumptions failed above. Assume that we're given a package name
pipe(
SanitizedAbsPath.make(`${projectDir}/node_modules/${pluginNameOrPath}/package.json`),
chain(readJsonFile),
)
),
map(value => value as Manifest),
);
export const getPackageName = (projectDir: SanitizedAbsPath.t) => (pluginNameOrPath: string) =>
pipe(
getPluginPackageJson(pluginNameOrPath, projectDir),
chainRej(err => {
if (isTaqError(err) && err.kind === `E_READFILE`) {
return resolve({ name: pluginNameOrPath });
}
return reject(err);
}),
map(manifest => manifest.name),
);
const addToPluginList = (pluginName: NpmPluginName, loadedConfig: LoadedConfig.t) =>
pipe(
getPluginPackageJson(pluginName, loadedConfig.projectDir),
chain((manifest: { name: string }) => {
const allPlugins = loadedConfig.plugins ?? [];
const existingPlugins = allPlugins.filter(plugin => plugin.name != manifest.name);
return NonEmptyString.make(manifest.name)
.pipe(
chain(name => InstalledPlugin.make({ name, type: 'npm' })),
)
.pipe(
map(installedPlugin => [...existingPlugins, installedPlugin]),
);
}),
chain(plugins =>
Config.make({
...loadedConfig,
plugins,
})
),
chain(config =>
pipe(
writeJsonFile(loadedConfig.configFile)(config),
chain(_ =>
LoadedConfig.make({
...loadedConfig,
...config,
})
),
)
),
);
const rmFromPluginList = (loadedConfig: LoadedConfig.t, parsedArgs: SanitizedArgs.UninstallTaskArgs) =>
pipe(
getPluginName(parsedArgs.pluginName),
pluginName => loadedConfig.plugins?.filter(plugin => plugin.name != pluginName.toString()),
plugins =>
pipe(
Config.make({
...loadedConfig,
plugins,
}),
chain(writeJsonFile(loadedConfig.configFile)),
chain(_ =>
LoadedConfig.make({
...loadedConfig,
plugins,
})
),
),
);
const purgeEphemeralState =
(env: EnvVars, i18n: i18n, parsedArgs: SanitizedArgs.InstallTaskArgs) => (config: LoadedConfig.t) => {
const pluginsLib = initPlugins({
config,
env,
i18n,
parsedArgs,
stderr: Deno.stderr,
stdout: Deno.stdout,
});
return pipe(
pluginsLib.getStateAbspath(),
chain(rm),
map(() => config),
);
};
export const installPlugin = (
config: LoadedConfig.t,
projectDir: SanitizedAbsPath.t,
i18n: i18n,
env: EnvVars,
parsedArgs: SanitizedArgs.InstallTaskArgs,
): Future<TaqError.t, LoadedConfig.t> =>
pipe(
requireNPM(projectDir, i18n),
chain(_ => exec('npm install -D <%= it.plugin %>', { plugin: parsedArgs.pluginName }, false, projectDir)),
chain(_result => {
// TODO: we need to check whether the plugin installation was actually successful
// The plugin name could look like this: @taqueria/[email protected]
// We need to trim @1.2.3 from the end
const pluginName = getPluginName(parsedArgs.pluginName);
// Note, pluginName could be something like @taqueria/plugin-ligo
// or ../taqueria-plugin-ligo. Thus, we still need to determine
// what the real package name is
return addToPluginList(pluginName, config);
}),
chain(purgeEphemeralState(env, i18n, parsedArgs)),
);
export const uninstallPlugin = (
config: LoadedConfig.t,
projectDir: SanitizedAbsPath.t,
i18n: i18n,
env: EnvVars,
parsedArgs: SanitizedArgs.UninstallTaskArgs,
) =>
pipe(
requireNPM(projectDir, i18n),
chain(() => exec('npm uninstall -D <%= it.plugin %>', { plugin: parsedArgs.pluginName }, false, projectDir)),
chain(() => rmFromPluginList(config, parsedArgs)),
chain(purgeEphemeralState(env, i18n, parsedArgs)),
);