|
3 | 3 | * Licensed under the MIT License. See License.txt in the project root for license information. |
4 | 4 | *--------------------------------------------------------------------------------------------*/ |
5 | 5 |
|
6 | | -'use strict'; |
| 6 | +import fs from 'node:fs'; |
| 7 | +import path from 'node:path'; |
| 8 | +import { fileURLToPath } from 'node:url'; |
7 | 9 |
|
8 | | -var path = require('path'); |
9 | | -var fs = require('fs'); |
| 10 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
10 | 11 |
|
11 | | -function getVersion(moduleName) { |
12 | | - var packageJSONPath = path.join(__dirname, '..', 'node_modules', moduleName, 'package.json'); |
13 | | - return readFile(packageJSONPath).then(function (content) { |
14 | | - try { |
15 | | - return JSON.parse(content).version; |
16 | | - } catch (e) { |
17 | | - return Promise.resolve(null); |
18 | | - } |
19 | | - }); |
| 12 | +async function getVersion(moduleName) { |
| 13 | + const packageJSONPath = path.join(__dirname, '..', 'node_modules', moduleName, 'package.json'); |
| 14 | + const content = await readFile(packageJSONPath); |
| 15 | + try { |
| 16 | + return JSON.parse(content).version; |
| 17 | + } catch { |
| 18 | + return null; |
| 19 | + } |
20 | 20 | } |
21 | 21 |
|
22 | | -function readFile(path) { |
23 | | - return new Promise((s, e) => { |
24 | | - fs.readFile(path, (err, res) => { |
| 22 | +function readFile(filePath) { |
| 23 | + return new Promise((resolve, reject) => { |
| 24 | + fs.readFile(filePath, (err, res) => { |
25 | 25 | if (err) { |
26 | | - e(err); |
| 26 | + reject(err); |
27 | 27 | } else { |
28 | | - s(res.toString()); |
| 28 | + resolve(res.toString()); |
29 | 29 | } |
30 | 30 | }); |
31 | 31 | }); |
32 | | - |
33 | 32 | } |
34 | 33 |
|
35 | | -function update(moduleName, repoPath, dest, addHeader, patch) { |
36 | | - var contentPath = path.join(__dirname, '..', 'node_modules', moduleName, repoPath); |
| 34 | +async function update(moduleName, repoPath, dest, addHeader, patch) { |
| 35 | + const contentPath = path.join(__dirname, '..', 'node_modules', moduleName, repoPath); |
37 | 36 | console.log('Reading from ' + contentPath); |
38 | | - return readFile(contentPath).then(function (content) { |
39 | | - return getVersion(moduleName).then(function (version) { |
40 | | - let header = ''; |
41 | | - if (addHeader) { |
42 | | - header = '// copied from js-beautify/' + repoPath + '\n'; |
43 | | - if (version) { |
44 | | - header += '// version: ' + version + '\n'; |
45 | | - } |
46 | | - } |
47 | | - try { |
48 | | - if (patch) { |
49 | | - content = patch(content); |
50 | | - } |
51 | | - fs.writeFileSync(dest, header + content); |
52 | | - if (version) { |
53 | | - console.log('Updated ' + path.basename(dest) + ' (' + version + ')'); |
54 | | - } else { |
55 | | - console.log('Updated ' + path.basename(dest)); |
56 | | - } |
57 | | - } catch (e) { |
58 | | - console.error(e); |
| 37 | + try { |
| 38 | + let content = await readFile(contentPath); |
| 39 | + const version = await getVersion(moduleName); |
| 40 | + let header = ''; |
| 41 | + if (addHeader) { |
| 42 | + header = '// copied from js-beautify/' + repoPath + '\n'; |
| 43 | + if (version) { |
| 44 | + header += '// version: ' + version + '\n'; |
59 | 45 | } |
60 | | - }); |
61 | | - |
62 | | - }, console.error); |
| 46 | + } |
| 47 | + if (patch) { |
| 48 | + content = patch(content); |
| 49 | + } |
| 50 | + fs.writeFileSync(dest, header + content); |
| 51 | + if (version) { |
| 52 | + console.log('Updated ' + path.basename(dest) + ' (' + version + ')'); |
| 53 | + } else { |
| 54 | + console.log('Updated ' + path.basename(dest)); |
| 55 | + } |
| 56 | + } catch (e) { |
| 57 | + console.error(e); |
| 58 | + } |
63 | 59 | } |
64 | 60 |
|
65 | | -update('js-beautify', 'js/lib/beautify-css.js', './src/beautify/beautify-css.js', true); |
66 | | -update('js-beautify', 'LICENSE', './src/beautify/beautify-license'); |
67 | | - |
68 | | -// ESM version |
69 | | -update('js-beautify', 'js/lib/beautify-css.js', './src/beautify/esm/beautify-css.js', true, function (contents) { |
70 | | - let topLevelFunction = '(function() {'; |
71 | | - let outputVar = 'var legacy_beautify_css'; |
72 | | - let footer = 'var css_beautify = legacy_beautify_css;'; |
73 | | - let index1 = contents.indexOf(topLevelFunction); |
74 | | - let index2 = contents.indexOf(outputVar, index1); |
75 | | - let index3 = contents.indexOf(footer, index2); |
76 | | - if (index1 === -1) { |
77 | | - throw new Error(`Problem patching beautify.css for ESM: '${topLevelFunction}' not found.`); |
78 | | - } |
79 | | - if (index2 === -1) { |
80 | | - throw new Error(`Problem patching beautify.css for ESM: '${outputVar}' not found after '${topLevelFunction}'.`); |
81 | | - } |
82 | | - if (index3 === -1) { |
83 | | - throw new Error(`Problem patching beautify.css for ESM: '${footer}' not found after '${outputVar}'.`); |
84 | | - } |
85 | | - return contents.substring(0, index1) + |
86 | | - contents.substring(index2, index3) + |
87 | | - `\nexport var css_beautify = legacy_beautify_css;`; |
88 | | -}); |
| 61 | +await Promise.all([ |
| 62 | + update('js-beautify', 'LICENSE', './src/beautify/beautify-license'), |
| 63 | + // Write the ESM-compatible variant directly to the main source path. |
| 64 | + update('js-beautify', 'js/lib/beautify-css.js', './src/beautify/beautify-css.js', true, (contents) => { |
| 65 | + const topLevelFunction = '(function() {'; |
| 66 | + const outputVar = 'var legacy_beautify_css'; |
| 67 | + const footer = 'var css_beautify = legacy_beautify_css;'; |
| 68 | + const index1 = contents.indexOf(topLevelFunction); |
| 69 | + const index2 = contents.indexOf(outputVar, index1); |
| 70 | + const index3 = contents.indexOf(footer, index2); |
| 71 | + if (index1 === -1) { |
| 72 | + throw new Error(`Problem patching beautify.css for ESM: '${topLevelFunction}' not found.`); |
| 73 | + } |
| 74 | + if (index2 === -1) { |
| 75 | + throw new Error(`Problem patching beautify.css for ESM: '${outputVar}' not found after '${topLevelFunction}'.`); |
| 76 | + } |
| 77 | + if (index3 === -1) { |
| 78 | + throw new Error(`Problem patching beautify.css for ESM: '${footer}' not found after '${outputVar}'.`); |
| 79 | + } |
| 80 | + return contents.substring(0, index1) + |
| 81 | + contents.substring(index2, index3) + |
| 82 | + '\nexport var css_beautify = legacy_beautify_css;'; |
| 83 | + }) |
| 84 | +]); |
0 commit comments