UNPKG

projectz

Version:

Stop wasting time syncing and updating your project's README and Package Files!

260 lines (259 loc) 10.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getInstallInstructions = void 0; // external const render_1 = require("@bevry/render"); const util_js_1 = require("./util.js"); function getNpmInstructionList(data, commands, local) { const label = `Executable${commands.length === 1 ? '' : 's'}`; let importStatement = ''; let requireStatement = ''; if (local && data.main) { if (Array.isArray(data.keywords) && data.keywords.includes('export-default')) { importStatement = `import pkg from ('${data.name}')`; requireStatement = `const pkg = require('${data.name}').default`; } else { importStatement = `import * as pkg from ('${data.name}')`; requireStatement = `const pkg = require('${data.name}')`; } } return (0, render_1.mul)([ (0, render_1.t)([ 'Install:', (0, render_1.mcode)(`npm install ${local ? '--save' : '--global'} ${data.name}`), ]), (0, render_1.i)(commands.length, () => (0, render_1.t)([ `${label}:`, commands .map((command) => (local ? `npx ${command}` : command)) .map(render_1.mcode) .join(', '), ])), (0, render_1.i)(importStatement, () => (0, render_1.t)(['Import:', (0, render_1.mcode)(importStatement)])), (0, render_1.i)(requireStatement, () => (0, render_1.t)(['Require:', (0, render_1.mcode)(requireStatement)])), ]); } function getNpmInstructions(data) { const commands = typeof data.bin === 'string' ? [data.name] : Object.keys(data.bin || {}); return (0, render_1.lines)([ (0, render_1.mh3)((0, render_1.ma)({ inner: 'npm', url: 'https://npmjs.com', title: 'npm is a package manager for javascript', })), (0, render_1.i)(commands.length, () => (0, render_1.mh4)('Install Globally')), (0, render_1.i)(commands.length, () => getNpmInstructionList(data, commands, false)), (0, render_1.i)(commands.length, () => (0, render_1.mh4)('Install Locally')), getNpmInstructionList(data, commands, true), ]); } function getDenoInstructions(data) { const url = `https://unpkg.com/${data.name}@^${data.version}` + (data.deno ? `/${data.deno}` : ''); const importer = Array.isArray(data.keywords) && data.keywords.includes('export-default') ? `pkg` : '* as pkg'; const imports = `import ${importer} from '${url}'`; return (0, render_1.lines)([ (0, render_1.mh3)((0, render_1.ma)({ inner: 'Deno', url: 'https://deno.land', title: 'Deno is a secure runtime for JavaScript and TypeScript, it is an alternative for Node.js', })), (0, render_1.mcodeblock)('typescript', `${imports}`), ]); } function getUnpkgInstructions(data) { const url = `//unpkg.com/${data.name}@^${data.version}`; const importer = Array.isArray(data.keywords) && data.keywords.includes('export-default') ? `pkg` : '* as pkg'; const imports = `import ${importer} from '${url}'`; return (0, render_1.lines)([ (0, render_1.mh3)((0, render_1.ma)({ inner: 'unpkg', url: 'https://unpkg.com', title: 'unpkg is a fast, global content delivery network for everything on npm', })), (0, render_1.mcodeblock)('html', [ '<script type="module">', ` ${imports}`, `</script>`, ]), ]); } function getSkypackInstructions(data) { const url = `//cdn.skypack.dev/${data.name}@^${data.version}`; const importer = Array.isArray(data.keywords) && data.keywords.includes('export-default') ? `pkg` : '* as pkg'; const imports = `import ${importer} from '${url}'`; return (0, render_1.lines)([ (0, render_1.mh3)((0, render_1.ma)({ inner: 'Skypack', url: 'https://www.skypack.dev', title: 'Skypack is a JavaScript Delivery Network for modern web apps', })), (0, render_1.mcodeblock)('html', [ '<script type="module">', ` ${imports}`, `</script>`, ]), ]); } function getJspmInstructions(data) { const url = `//dev.jspm.io/${data.name}@${data.version}`; const importer = Array.isArray(data.keywords) && data.keywords.includes('export-default') ? `pkg` : '* as pkg'; const imports = `import ${importer} from '${url}'`; return (0, render_1.lines)([ (0, render_1.mh3)((0, render_1.ma)({ inner: 'jspm', url: 'https://jspm.io', title: 'Native ES Modules CDN', })), (0, render_1.mcodeblock)('html', [ '<script type="module">', ` ${imports}`, `</script>`, ]), ]); } function getTypeScriptInstructions() { return (0, render_1.lines)([ (0, util_js_1.hydrateTextWithLinks)((0, render_1.mh3)('TypeScript')), (0, util_js_1.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 accomplish this via your `tsconfig.json` file like so:'), (0, render_1.mcodeblock)('json', JSON.stringify({ compilerOptions: { maxNodeModuleJsDepth: 5, }, }, null, ' ')), ]); } function getComponentInstructions(data) { return (0, render_1.lines)([ (0, render_1.mh3)((0, render_1.ma)({ inner: 'Component', url: 'https://github.com/componentjs/component', title: 'Frontend package manager and build tool for modular web applications', })), (0, render_1.mul)(['Install: ' + (0, render_1.mcode)(`component install ${data.name}`)]), ]); } function getBowerInstructions(data) { return (0, render_1.lines)([ (0, render_1.mh3)((0, render_1.ma)({ inner: 'Bower', url: 'https://bower.io', title: 'A package manager for the web', })), (0, render_1.mul)(['Install: ' + (0, render_1.mcode)(`bower install ${data.name}`)]), ]); } function getEditionsInstructions(data) { if (!data.editions.length) return ''; let hasDefaultEdition = false; const editions = []; for (const edition of data.editions) { const entryParts = []; if (edition.directory) { entryParts.push(edition.directory); } if (edition.entry) { // handle the editions standard 1.3 and below // can't use substring, as we don't know if we have 1.3 and below or not if (edition.directory) { entryParts.push(edition.entry.replace(edition.directory.length + '/', '')); } else { entryParts.push(edition.entry); } } const entry = entryParts.join('/'); if (entry === data.main) { hasDefaultEdition = true; editions.push((0, render_1.t)([(0, render_1.mcode)(data.name), 'aliases', (0, render_1.mcode)(`${data.name}/${data.main}`)])); } editions.push((0, render_1.t)([(0, render_1.mcode)(`${data.name}/${entry}`), 'is', edition.description])); } // Autoloaders if (!hasDefaultEdition) { if ('editions' in data.dependencies) { editions.unshift((0, render_1.t)([ (0, render_1.mcode)(data.name), 'aliases', (0, render_1.mcode)(`${data.name}/${data.main}`), `which uses the Editions Autoloader to automatically select the correct edition for the consumer's environment`, ])); } else if ('esnextguardian' in data.dependencies) { editions.unshift((0, render_1.t)([ (0, render_1.mcode)(data.name), 'aliases', (0, render_1.mcode)(`${data.name}/${data.main}`), 'which uses ESNextGuardian to automatically select the correct edition for the consumers environment', ])); } } // Compile result const result = (0, render_1.lines)([ (0, render_1.mh3)('Editions'), 'This package is published with the following editions:', (0, render_1.mul)(editions), ]); // Add links return (0, util_js_1.hydrateTextWithLinks)(result); } // Define function getInstallInstructions(data) { const parts = [(0, render_1.mh2)('Install')]; // DocPad const prefix = 'docpad-plugin-'; if (data.name.startsWith(prefix)) { const pluginName = data.name.substring(prefix.length); parts.push((0, render_1.t)([ 'Install this DocPad plugin by entering', (0, render_1.mcode)(`docpad install ${pluginName}`), 'into your terminal.', ])); } else { // Node if (data.filenamesForPackageFiles.package) { parts.push(getNpmInstructions(data)); // Deno if (data.deno) { parts.push(getDenoInstructions(data)); } // Browser if (data.browsers) { if (data.module) { parts.push(getSkypackInstructions(data)); parts.push(getUnpkgInstructions(data)); } parts.push(getJspmInstructions(data)); } } // Component if (data.filenamesForPackageFiles.component) { parts.push(getComponentInstructions(data)); } // Bower if (data.filenamesForPackageFiles.bower) { parts.push(getBowerInstructions(data)); } // Editions if (data.editions) { parts.push(getEditionsInstructions(data)); } } if (data.main && data.devDependencies && data.devDependencies.jsdoc) { parts.push(getTypeScriptInstructions()); } return (0, render_1.lines)(parts); } exports.getInstallInstructions = getInstallInstructions;