|
| 1 | +const { |
| 2 | + readdirSync, |
| 3 | + writeFileSync, |
| 4 | + statSync, |
| 5 | + readFileSync, |
| 6 | + existsSync, |
| 7 | +} = require('fs'); |
| 8 | + |
| 9 | +const LICENSE = `Copyright (c) ${new Date().getFullYear()} Forbes Lindesay |
| 10 | +
|
| 11 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | +of this software and associated documentation files (the "Software"), to deal |
| 13 | +in the Software without restriction, including without limitation the rights |
| 14 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | +copies of the Software, and to permit persons to whom the Software is |
| 16 | +furnished to do so, subject to the following conditions: |
| 17 | +
|
| 18 | +The above copyright notice and this permission notice shall be included in |
| 19 | +all copies or substantial portions of the Software. |
| 20 | +
|
| 21 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | +THE SOFTWARE.`; |
| 28 | + |
| 29 | +const packageNames = []; |
| 30 | +const typeScriptPackages = []; |
| 31 | +const packageDirectories = readdirSync(__dirname + '/../packages') |
| 32 | + .filter(directory => |
| 33 | + statSync(__dirname + '/../packages/' + directory).isDirectory() |
| 34 | + ) |
| 35 | + .sort(); |
| 36 | +packageDirectories.forEach(directory => { |
| 37 | + if (!existsSync(__dirname + '/../packages/' + directory + '/LICENSE')) { |
| 38 | + writeFileSync( |
| 39 | + __dirname + '/../packages/' + directory + '/LICENSE', |
| 40 | + LICENSE |
| 41 | + ); |
| 42 | + } |
| 43 | + let pkg = {}; |
| 44 | + try { |
| 45 | + pkg = JSON.parse( |
| 46 | + readFileSync( |
| 47 | + __dirname + '/../packages/' + directory + '/package.json', |
| 48 | + 'utf8' |
| 49 | + ) |
| 50 | + ); |
| 51 | + } catch (ex) { |
| 52 | + if (ex.code !== 'ENOENT') { |
| 53 | + throw ex; |
| 54 | + } |
| 55 | + } |
| 56 | + const before = JSON.stringify(pkg); |
| 57 | + if (!pkg.name) { |
| 58 | + pkg.name = directory; |
| 59 | + } |
| 60 | + packageNames.push(pkg.name); |
| 61 | + const after = JSON.stringify(pkg); |
| 62 | + if (before !== after) { |
| 63 | + writeFileSync( |
| 64 | + __dirname + '/../packages/' + directory + '/package.json', |
| 65 | + JSON.stringify(pkg, null, ' ') + '\n' |
| 66 | + ); |
| 67 | + } |
| 68 | + if (existsSync(__dirname + '/../packages/' + directory + '/tsconfig.json')) { |
| 69 | + typeScriptPackages.push(directory); |
| 70 | + const deps = [ |
| 71 | + ...Object.keys(pkg.dependencies || {}), |
| 72 | + ...Object.keys(pkg.devDependencies || {}), |
| 73 | + ] |
| 74 | + .filter(dep => |
| 75 | + existsSync(__dirname + '/../packages/' + dep + '/tsconfig.json') |
| 76 | + ) |
| 77 | + .map( |
| 78 | + dep => |
| 79 | + `\n {"path": ${JSON.stringify( |
| 80 | + `../${dep.substr(`@databases/`.length)}` |
| 81 | + )}},` |
| 82 | + ) |
| 83 | + .join(``); |
| 84 | + writeFileSync( |
| 85 | + __dirname + '/../packages/' + directory + '/tsconfig.json', |
| 86 | + `{ |
| 87 | + "extends": "../../tsconfig.json", |
| 88 | + "compilerOptions": { |
| 89 | + "composite": true, |
| 90 | + "rootDir": "src", |
| 91 | + "outDir": "lib", |
| 92 | + "tsBuildInfoFile": "lib/tsconfig.tsbuildinfo", |
| 93 | + }, |
| 94 | + "references": ${deps.length ? `[${deps}\n ],` : `[],`} |
| 95 | +} |
| 96 | +` |
| 97 | + ); |
| 98 | + } |
| 99 | +}); |
| 100 | + |
| 101 | +writeFileSync( |
| 102 | + `scripts/tsconfig.json`, |
| 103 | + `{ |
| 104 | + "extends": "../tsconfig.json", |
| 105 | + "references": [${typeScriptPackages |
| 106 | + .map(n => `\n {"path": ${JSON.stringify(`../packages/${n}`)}},`) |
| 107 | + .join(``)} |
| 108 | + ], |
| 109 | +}` |
| 110 | +); |
| 111 | +const [README_HEADER, _table, README_FOOTER] = readFileSync( |
| 112 | + __dirname + '/../README.md', |
| 113 | + 'utf8' |
| 114 | +).split('<!-- VERSION_TABLE -->'); |
| 115 | + |
| 116 | +const versionsTable = ` |
| 117 | +Package Name | Version |
| 118 | +-------------|-------- |
| 119 | +${packageNames |
| 120 | + .sort() |
| 121 | + .map( |
| 122 | + name => |
| 123 | + `${name} | [](https://www.npmjs.com/package/${name})` |
| 124 | + ) |
| 125 | + .join('\n')} |
| 126 | +`; |
| 127 | +writeFileSync( |
| 128 | + __dirname + '/../README.md', |
| 129 | + [README_HEADER, versionsTable, README_FOOTER || ''].join( |
| 130 | + '<!-- VERSION_TABLE -->' |
| 131 | + ) |
| 132 | +); |
0 commit comments