|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | +import lockfile from 'lockfile'; |
| 4 | +import util from 'util'; |
| 5 | +import os from 'os'; |
| 6 | +import crypto from 'crypto'; |
| 7 | + |
| 8 | +const lfUnlock = util.promisify(lockfile.unlock); |
| 9 | + |
| 10 | +/** |
| 11 | + * Check if file exists or not using fs API. |
| 12 | + */ |
| 13 | +export function fileExists(filepath: string): boolean { |
| 14 | + try { |
| 15 | + // tslint:disable-next-line:non-literal-fs-path |
| 16 | + return fs.statSync(filepath).isFile(); |
| 17 | + } catch (_) { |
| 18 | + return false; |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Check whether current working directory is a typescript project. |
| 24 | + * |
| 25 | + * |
| 26 | + * @param cwd Current working directory. |
| 27 | + * @returns True if tsconfig is found, false otherwiise. |
| 28 | + */ |
| 29 | +export function hasTypeScript(cwd: string): [boolean, string] { |
| 30 | + const tsconfigPath = path.resolve(cwd, './tsconfig.json'); |
| 31 | + return [fileExists(tsconfigPath), tsconfigPath]; |
| 32 | +} |
| 33 | + |
| 34 | +export const WORDPRESS_NAMESPACE = '@wordpress/'; |
| 35 | +export const BUNDLED_PACKAGES = ['@wordpress/icons', '@wordpress/interface']; |
| 36 | + |
| 37 | +/** |
| 38 | + * Given a string, returns a new string with dash separators converted to |
| 39 | + * camelCase equivalent. This is not as aggressive as `_.camelCase` in |
| 40 | + * converting to uppercase, where Lodash will also capitalize letters |
| 41 | + * following numbers. |
| 42 | + * |
| 43 | + * @see {https://github.com/WordPress/gutenberg/blob/c047e2716149c794eebff3c2c002f66a6f546f59/packages/dependency-extraction-webpack-plugin/lib/util.js#L83} |
| 44 | + * |
| 45 | + * @param {string} input Input dash-delimited string. |
| 46 | + * @return {string} Camel-cased string. |
| 47 | + */ |
| 48 | +function camelCaseDash(input: string): string { |
| 49 | + return input.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase()); |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Wpackio specific request to global transformation. This is used in |
| 54 | + * @wordpress/dependency-extraction-webpack-plugin. |
| 55 | + * |
| 56 | + * Unlike the default one, it doesn't transform react/reactdom, moment, jQuery |
| 57 | + * etc. |
| 58 | + * |
| 59 | + * Transform @wordpress dependencies: |
| 60 | + * - request `@wordpress/api-fetch` becomes `[ 'wp', 'apiFetch' ]` |
| 61 | + * - request `@wordpress/i18n` becomes `[ 'wp', 'i18n' ]` |
| 62 | + * |
| 63 | + * @param request Module request (the module name in `import from`) to be transformed |
| 64 | + * @returns The resulting external definition. Return `undefined` |
| 65 | + * to ignore the request. Return `string|string[]` to map the request to an external. |
| 66 | + */ |
| 67 | +export function wpackioRequestsToExternals( |
| 68 | + request: string |
| 69 | +): string | string[] | undefined { |
| 70 | + if (BUNDLED_PACKAGES.includes(request)) { |
| 71 | + return undefined; |
| 72 | + } |
| 73 | + |
| 74 | + if (request.startsWith(WORDPRESS_NAMESPACE)) { |
| 75 | + return ['wp', camelCaseDash(request.substring(WORDPRESS_NAMESPACE.length))]; |
| 76 | + } |
| 77 | + |
| 78 | + return undefined; |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Default request to global transformation |
| 83 | + * |
| 84 | + * Transform @wordpress dependencies: |
| 85 | + * - request `@wordpress/api-fetch` becomes `[ 'wp', 'apiFetch' ]` |
| 86 | + * - request `@wordpress/i18n` becomes `[ 'wp', 'i18n' ]` |
| 87 | + * |
| 88 | + * @param {string} request Module request (the module name in `import from`) to be transformed |
| 89 | + * @return {string|string[]|undefined} The resulting external definition. Return `undefined` |
| 90 | + * to ignore the request. Return `string|string[]` to map the request to an external. |
| 91 | + */ |
| 92 | +export function defaultRequestToExternal( |
| 93 | + request: string |
| 94 | +): string | string[] | undefined { |
| 95 | + switch (request) { |
| 96 | + case 'moment': |
| 97 | + return request; |
| 98 | + |
| 99 | + case '@babel/runtime/regenerator': |
| 100 | + return 'regeneratorRuntime'; |
| 101 | + |
| 102 | + case 'lodash': |
| 103 | + case 'lodash-es': |
| 104 | + return 'lodash'; |
| 105 | + |
| 106 | + case 'jquery': |
| 107 | + return 'jQuery'; |
| 108 | + |
| 109 | + case 'react': |
| 110 | + return 'React'; |
| 111 | + |
| 112 | + case 'react-dom': |
| 113 | + return 'ReactDOM'; |
| 114 | + |
| 115 | + default: |
| 116 | + return wpackioRequestsToExternals(request); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Wpackio specific request to WordPress script handle transformation |
| 122 | + * |
| 123 | + * Transform @wordpress dependencies: |
| 124 | + * - request `@wordpress/i18n` becomes `wp-i18n` |
| 125 | + * - request `@wordpress/escape-html` becomes `wp-escape-html` |
| 126 | + * |
| 127 | + * @param {string} request Module request (the module name in `import from`) to be transformed |
| 128 | + * @return {string|undefined} WordPress script handle to map the request to. Return `undefined` |
| 129 | + * to use the same name as the module. |
| 130 | + */ |
| 131 | +export function wpackioRequestToHandle(request: string): string | undefined { |
| 132 | + if (request.startsWith(WORDPRESS_NAMESPACE)) { |
| 133 | + return `wp-${request.substring(WORDPRESS_NAMESPACE.length)}`; |
| 134 | + } |
| 135 | + return undefined; |
| 136 | +} |
| 137 | + |
| 138 | +/** |
| 139 | + * Default request to WordPress script handle transformation |
| 140 | + * |
| 141 | + * Transform @wordpress dependencies: |
| 142 | + * - request `@wordpress/i18n` becomes `wp-i18n` |
| 143 | + * - request `@wordpress/escape-html` becomes `wp-escape-html` |
| 144 | + * |
| 145 | + * @param {string} request Module request (the module name in `import from`) to be transformed |
| 146 | + * @return {string|undefined} WordPress script handle to map the request to. Return `undefined` |
| 147 | + * to use the same name as the module. |
| 148 | + */ |
| 149 | +export function defaultRequestToHandle(request: string): undefined | string { |
| 150 | + switch (request) { |
| 151 | + case '@babel/runtime/regenerator': |
| 152 | + return 'wp-polyfill'; |
| 153 | + |
| 154 | + case 'lodash-es': |
| 155 | + return 'lodash'; |
| 156 | + default: |
| 157 | + return wpackioRequestToHandle(request); |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +/** |
| 162 | + * Get the name of the file from a file path. |
| 163 | + * |
| 164 | + * @param name Full path of the filename. |
| 165 | + * @returns Just the name of the file. |
| 166 | + */ |
| 167 | +export function basename(name: string) { |
| 168 | + if (!name.includes('/')) { |
| 169 | + return name; |
| 170 | + } |
| 171 | + return name.substr(name.lastIndexOf('/') + 1); |
| 172 | +} |
| 173 | + |
| 174 | +function md5(data: crypto.BinaryLike | string) { |
| 175 | + return crypto.createHash('md5').update(data).digest('hex'); |
| 176 | +} |
| 177 | + |
| 178 | +/** |
| 179 | + * Build a file path to a lock file in the tmp directory |
| 180 | + * |
| 181 | + * @param {string} filename |
| 182 | + */ |
| 183 | +export function getLockFilename(filename: string) { |
| 184 | + const name = path.basename(filename); |
| 185 | + const dirHash = md5(path.dirname(filename)); |
| 186 | + |
| 187 | + return path.join(os.tmpdir(), `${dirHash}-${name}.lock`); |
| 188 | +} |
| 189 | + |
| 190 | +/** |
| 191 | + * Create a lockfile (async) |
| 192 | + * |
| 193 | + * @param {string} filename |
| 194 | + */ |
| 195 | +export async function lock(filename: string) { |
| 196 | + await new Promise<void>((resolve, reject) => { |
| 197 | + lockfile.lock( |
| 198 | + getLockFilename(filename), |
| 199 | + { wait: 6000, retryWait: 100, stale: 5000, retries: 100 }, |
| 200 | + err => { |
| 201 | + if (err) { |
| 202 | + reject(err); |
| 203 | + return; |
| 204 | + } |
| 205 | + resolve(); |
| 206 | + } |
| 207 | + ); |
| 208 | + }); |
| 209 | +} |
| 210 | + |
| 211 | +/** |
| 212 | + * Remove a lockfile (async) |
| 213 | + * |
| 214 | + * @param {string} filename |
| 215 | + */ |
| 216 | +export async function unlock(filename: string) { |
| 217 | + await lfUnlock(getLockFilename(filename)); |
| 218 | +} |
0 commit comments