UNPKG

13.8 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.getInstallInstructions = void 0;
4const util_js_1 = require("./util.js");
5function hydrateTextWithLinks(text) {
6 const linksArray = [
7 {
8 text: 'Deno',
9 url: 'https://deno.land',
10 title: 'Deno is a secure runtime for JavaScript and TypeScript, it is an alternative to Node.js',
11 },
12 {
13 text: 'Editions Autoloader',
14 url: 'https://github.com/bevry/editions',
15 title: 'You can use the Editions Autoloader to autoload the appropriate edition for your consumers environment',
16 },
17 {
18 text: 'Editions',
19 url: 'https://editions.bevry.me',
20 title: 'Editions are the best way to produce and consume packages you care about.',
21 },
22 {
23 text: 'ESNextGuardian',
24 url: 'https://github.com/bevry/esnextguardian',
25 title: "Loads ES6+ files if the user's environment supports it, otherwise gracefully fallback to ES5 files.",
26 },
27 {
28 text: "Babel's Polyfill",
29 url: 'https://babeljs.io/docs/usage/polyfill/',
30 title: 'A polyfill that emulates missing ECMAScript environment features',
31 },
32 {
33 text: 'Babel',
34 url: 'https://babeljs.io',
35 title: 'The compiler for writing next generation JavaScript',
36 },
37 {
38 text: 'CoffeeScript',
39 url: 'https://coffeescript.org',
40 title: 'CoffeeScript is a little language that compiles into JavaScript',
41 },
42 {
43 text: 'Require',
44 url: 'https://nodejs.org/dist/latest-v5.x/docs/api/modules.html',
45 title: 'Node/CJS Modules',
46 },
47 {
48 text: 'Import',
49 url: 'https://babeljs.io/docs/learn-es2015/#modules',
50 title: 'ECMAScript Modules',
51 },
52 {
53 text: 'ESNext',
54 url: 'https://en.wikipedia.org/wiki/ECMAScript#ES.Next',
55 title: 'ECMAScript Next',
56 },
57 {
58 text: 'ES2020',
59 url: 'https://en.wikipedia.org/wiki/ECMAScript#11th_Edition_–_ECMAScript_2020',
60 title: 'ECMAScript ES2020',
61 },
62 {
63 text: 'ES2019',
64 url: 'https://en.wikipedia.org/wiki/ECMAScript#10th_Edition_-_ECMAScript_2019',
65 title: 'ECMAScript ES2019',
66 },
67 {
68 text: 'ES2018',
69 url: 'https://en.wikipedia.org/wiki/ECMAScript#9th_Edition_-_ECMAScript_2018',
70 title: 'ECMAScript ES2018',
71 },
72 {
73 text: 'ES2017',
74 url: 'https://en.wikipedia.org/wiki/ECMAScript#8th_Edition_-_ECMAScript_2017',
75 title: 'ECMAScript ES2017',
76 },
77 {
78 text: 'ES2016',
79 url: 'https://en.wikipedia.org/wiki/ECMAScript#7th_Edition_-_ECMAScript_2016',
80 title: 'ECMAScript 2016',
81 },
82 {
83 text: 'ES2015',
84 url: 'https://babeljs.io/docs/en/learn#ecmascript-2015-features',
85 title: 'ECMAScript 2015',
86 },
87 {
88 text: 'JSDoc Comments',
89 url: 'http://usejsdoc.org',
90 title: 'JSDoc is an API documentation generator for JavaScript, similar to Javadoc or phpDocumentor',
91 },
92 {
93 text: 'Flow Type Comments',
94 url: 'http://flowtype.org/blog/2015/02/20/Flow-Comments.html',
95 title: 'Flow is a static type checker for JavaScript',
96 },
97 {
98 text: 'Flow Type',
99 url: 'http://flowtype.org',
100 title: 'Flow is a static type checker for JavaScript',
101 },
102 {
103 text: 'JSX',
104 url: 'https://facebook.github.io/jsx/',
105 title: 'XML/HTML inside your JavaScript',
106 },
107 {
108 text: 'Node.js',
109 url: 'https://nodejs.org',
110 title: "Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine",
111 },
112 {
113 text: 'TypeScript',
114 url: 'https://www.typescriptlang.org/',
115 title: 'TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. ',
116 },
117 ];
118 // build a map
119 const linksMap = {};
120 for (const link of linksArray) {
121 linksMap[link.text.toLowerCase()] = util_js_1.getLink(link);
122 }
123 // do the replacement
124 const linksMatch = new RegExp(linksArray.map((link) => link.text).join('|'), 'g');
125 return text.replace(linksMatch, function (match) {
126 return linksMap[match.toLowerCase()];
127 });
128}
129function getNpmInstructionList(data, commands, local) {
130 const label = `Executable${commands.length === 1 ? '' : 's'}`;
131 let importStatement = '';
132 let requireStatement = '';
133 if (local && data.main) {
134 if (Array.isArray(data.keywords) &&
135 data.keywords.includes('export-default')) {
136 importStatement = `import pkg from ('${data.name}')`;
137 requireStatement = `const pkg = require('${data.name}').default`;
138 }
139 else {
140 importStatement = `import * as pkg from ('${data.name}')`;
141 requireStatement = `const pkg = require('${data.name}')`;
142 }
143 }
144 return [
145 '<ul>',
146 `<li>Install: <code>npm install ${local ? '--save' : '--global'} ${data.name}</code></li>`,
147 commands.length
148 ? `<li>${label}: <code>${commands
149 .map((command) => (local ? `npx ${command}` : command))
150 .join('</code>, <code>')}</code></li>`
151 : '',
152 importStatement ? `<li>Import: <code>${importStatement}</code></li>` : '',
153 requireStatement
154 ? `<li>Require: <code>${requireStatement}</code></li>`
155 : '',
156 '</ul>',
157 ]
158 .filter((i) => i)
159 .join('\n');
160}
161function getNpmInstructions(data) {
162 const commands = typeof data.bin === 'string' ? [data.name] : Object.keys(data.bin || {});
163 return [
164 util_js_1.getLink({
165 text: '<h3>npm</h3>',
166 url: 'https://npmjs.com',
167 title: 'npm is a package manager for javascript',
168 }),
169 commands.length && '<h4>Install Globally</h4>',
170 commands.length && getNpmInstructionList(data, commands, false),
171 commands.length && '<h4>Install Locally</h4>',
172 getNpmInstructionList(data, commands, true),
173 ]
174 .filter((i) => i)
175 .join('\n');
176}
177function getDenoInstructions(data) {
178 const url = `https://unpkg.com/${data.name}@^${data.version}` +
179 (data.deno ? `/${data.deno}` : '');
180 const importer = Array.isArray(data.keywords) && data.keywords.includes('export-default')
181 ? `pkg`
182 : '* as pkg';
183 return [
184 util_js_1.getLink({
185 text: '<h3>Deno</h3>',
186 url: 'https://deno.land',
187 title: 'Deno is a secure runtime for JavaScript and TypeScript, it is an alternative for Node.js',
188 }),
189 '',
190 '``` typescript',
191 `import ${importer} from '${url}'`,
192 '```',
193 ].join('\n');
194}
195function getUnpkgInstructions(data) {
196 const url = `//unpkg.com/${data.name}@^${data.version}`;
197 const importer = Array.isArray(data.keywords) && data.keywords.includes('export-default')
198 ? `pkg`
199 : '* as pkg';
200 return [
201 util_js_1.getLink({
202 text: '<h3>unpkg</h3>',
203 url: 'https://unpkg.com',
204 title: 'unpkg is a fast, global content delivery network for everything on npm',
205 }),
206 '',
207 '``` html',
208 '<script type="module">',
209 ` import ${importer} from '${url}'`,
210 `</script>`,
211 '```',
212 ].join('\n');
213}
214function getSkypackInstructions(data) {
215 const url = `//cdn.skypack.dev/${data.name}@^${data.version}`;
216 const importer = Array.isArray(data.keywords) && data.keywords.includes('export-default')
217 ? `pkg`
218 : '* as pkg';
219 return [
220 util_js_1.getLink({
221 text: '<h3>Skypack</h3>',
222 url: 'https://www.skypack.dev',
223 title: 'Skypack is a JavaScript Delivery Network for modern web apps',
224 }),
225 '',
226 '``` html',
227 '<script type="module">',
228 ` import ${importer} from '${url}'`,
229 `</script>`,
230 '```',
231 ].join('\n');
232}
233function getJspmInstructions(data) {
234 const url = `//dev.jspm.io/${data.name}@${data.version}`;
235 const importer = Array.isArray(data.keywords) && data.keywords.includes('export-default')
236 ? `pkg`
237 : '* as pkg';
238 return [
239 util_js_1.getLink({
240 text: '<h3>jspm</h3>',
241 url: 'https://jspm.io',
242 title: 'Native ES Modules CDN',
243 }),
244 '',
245 '``` html',
246 '<script type="module">',
247 ` import ${importer} from '${url}'`,
248 `</script>`,
249 '```',
250 ].join('\n');
251}
252function getTypeScriptInstructions() {
253 return [
254 hydrateTextWithLinks('<h3>TypeScript</h3>'),
255 '',
256 hydrateTextWithLinks('This project provides its type information via inline JSDoc Comments. To make use of this in TypeScript, set your <code>maxNodeModuleJsDepth</code> compiler option to `5` or thereabouts. You can accomlish this via your `tsconfig.json` file like so:'),
257 '',
258 '``` json',
259 JSON.stringify({
260 compilerOptions: {
261 maxNodeModuleJsDepth: 5,
262 },
263 }, null, ' '),
264 '```',
265 ].join('\n');
266}
267function getComponentInstructions(data) {
268 return [
269 util_js_1.getLink({
270 text: '<h3>Component</h3>',
271 url: 'https://github.com/componentjs/component',
272 title: 'Frontend package manager and build tool for modular web applications',
273 }),
274 `<ul><li>Install: <code>component install ${data.name}</code></li></ul>`,
275 ].join('\n');
276}
277function getBowerInstructions(data) {
278 return [
279 util_js_1.getLink({
280 text: '<h3>Bower</h3>',
281 url: 'https://bower.io',
282 title: 'A package manager for the web',
283 }),
284 `<ul><li>Install: <code>bower install ${data.name}</code></li></ul>`,
285 ].join('\n');
286}
287function getEditionsInstructions(data) {
288 if (!data.editions.length)
289 return '';
290 let hasDefaultEdition = false;
291 const editions = [];
292 for (const edition of data.editions) {
293 const entryParts = [];
294 if (edition.directory) {
295 entryParts.push(edition.directory);
296 }
297 if (edition.entry) {
298 // handle the editions standard 1.3 and below
299 // can't use substring, as we don't know if we have 1.3 and below or not
300 if (edition.directory) {
301 entryParts.push(edition.entry.replace(edition.directory.length + '/', ''));
302 }
303 else {
304 entryParts.push(edition.entry);
305 }
306 }
307 const entry = entryParts.join('/');
308 if (entry === data.main) {
309 hasDefaultEdition = true;
310 editions.push(`<code>${data.name}</code> aliases <code>${data.name}/${data.main}</code>`);
311 }
312 editions.push(`<code>${data.name}/${entry}</code> is ${edition.description}`);
313 }
314 // Autoloaders
315 if (!hasDefaultEdition) {
316 if ('editions' in data.dependencies) {
317 editions.unshift(`<code>${data.name}</code> aliases <code>${data.name}/${data.main}</code> which uses the Editions Autoloader to automatically select the correct edition for the consumer's environment`);
318 }
319 else if ('esnextguardian' in data.dependencies) {
320 editions.unshift(`<code>${data.name}</code> aliases <code>${data.name}/${data.main}</code> which uses ESNextGuardian to automatically select the correct edition for the consumers environment`);
321 }
322 }
323 // Compile result
324 const result = `<h3>Editions</h3>\n\n<p>This package is published with the following editions:</p>\n\n<ul><li>${editions.join('</li>\n<li>')}</li></ul>`;
325 // Add links
326 return hydrateTextWithLinks(result);
327}
328// Define
329function getInstallInstructions(data) {
330 const parts = ['<h2>Install</h2>'];
331 // DocPad
332 const prefix = 'docpad-plugin-';
333 if (data.name.startsWith(prefix)) {
334 const pluginName = data.name.substring(prefix.length);
335 parts.push(`Install this DocPad plugin by entering <code>docpad install ${pluginName}</code> into your terminal.`);
336 }
337 else {
338 // Node
339 if (data.filenamesForPackageFiles.package) {
340 parts.push(getNpmInstructions(data));
341 // Deno
342 if (data.deno) {
343 parts.push(getDenoInstructions(data));
344 }
345 // Browser
346 if (data.browsers) {
347 if (data.module) {
348 parts.push(getSkypackInstructions(data));
349 parts.push(getUnpkgInstructions(data));
350 }
351 parts.push(getJspmInstructions(data));
352 }
353 }
354 // Component
355 if (data.filenamesForPackageFiles.component) {
356 parts.push(getComponentInstructions(data));
357 }
358 // Bower
359 if (data.filenamesForPackageFiles.bower) {
360 parts.push(getBowerInstructions(data));
361 }
362 // Editions
363 if (data.editions) {
364 parts.push(getEditionsInstructions(data));
365 }
366 }
367 if (data.main && data.devDependencies && data.devDependencies.jsdoc) {
368 parts.push(getTypeScriptInstructions());
369 }
370 return parts.filter((i) => i).join('\n\n');
371}
372exports.getInstallInstructions = getInstallInstructions;