|
| 1 | +const yargs = require('yargs'); |
| 2 | +const path = require('path'); |
| 3 | +const fs = require('fs'); |
| 4 | +const execSync = require('child_process').execSync; |
| 5 | +const ServiceModelFinder = require('./utils/ModelFinder').ServiceModelFinder; |
| 6 | +const clientNameRegex = require('./utils/constants').clientNameRegex; |
| 7 | + |
| 8 | +/** |
| 9 | + * This script will scan packages directory, recognize service client packages and re-generate them from models |
| 10 | + * If this command fails with cannot find model \'clientModuleIdentifier\', please run npm bootstrap && npm test first. |
| 11 | + */ |
| 12 | +const models = yargs |
| 13 | + .alias('m', 'models') |
| 14 | + .string('m') |
| 15 | + .default('m', path.join('..', '..', 'models')) |
| 16 | + .describe('m', 'the directory of models') |
| 17 | + .help() |
| 18 | + .coerce('m', (directory) => { |
| 19 | + return path.normalize(path.join(__filename, path.normalize(directory))); |
| 20 | + }) |
| 21 | + .argv |
| 22 | + .models; |
| 23 | + |
| 24 | +console.info('models directory: ', models); |
| 25 | + |
| 26 | +const existingServiceClients = grabExistingClients(); |
| 27 | +console.info('existing service clients: ', existingServiceClients.map(item => path.parse(item).base)); |
| 28 | +console.info('generating service clients...'); |
| 29 | +const serviceModelFinder = new ServiceModelFinder(models); |
| 30 | +for (const serviceClient of existingServiceClients) { |
| 31 | + const clientName = path.parse(serviceClient).base; |
| 32 | + const models = serviceModelFinder.findModelsForService(clientName); |
| 33 | + const [_, serviceId, runtime] = clientNameRegex.exec(clientName); |
| 34 | + console.info(`generating ${runtime} client from model at ${models.service}`) |
| 35 | + generateClient(models, runtime); |
| 36 | +} |
| 37 | + |
| 38 | +console.log('done!'); |
| 39 | + |
| 40 | + |
| 41 | +function grabExistingClients() { |
| 42 | + const packagesDir = path.join(path.dirname(__dirname), 'packages'); |
| 43 | + const clientPackagesPaths = []; |
| 44 | + for (const package of fs.readdirSync(packagesDir)) { |
| 45 | + const packageDir = path.join(packagesDir, package); |
| 46 | + if (fs.statSync(packageDir).isDirectory() && isClientPackage(packageDir)) { |
| 47 | + clientPackagesPaths.push(packageDir); |
| 48 | + } |
| 49 | + } |
| 50 | + return clientPackagesPaths; |
| 51 | +} |
| 52 | + |
| 53 | +function isClientPackage(directory) { |
| 54 | + const baseName = path.parse(directory).base; |
| 55 | + if (!clientNameRegex.test(baseName)) return false; |
| 56 | + const clientFiles = [ |
| 57 | + {base: 'commands', isFile: false}, |
| 58 | + {base: 'model', isFile: false}, |
| 59 | + {base: 'types', isFile: false}, |
| 60 | + {base: `(\\w+)Client\\.ts`, isFile: true}, |
| 61 | + {base: `(\\w+)Configuration\\.ts`, isFile: true}, |
| 62 | + {base: `index\\.ts`, isFile: true}, |
| 63 | + ] |
| 64 | + try { |
| 65 | + const files = fs.readdirSync(directory); |
| 66 | + for (const clientFilePattern of clientFiles) { |
| 67 | + const matchedFile = arrayFindPattern(files, clientFilePattern.base); |
| 68 | + if ( |
| 69 | + !matchedFile && |
| 70 | + fs.statSync(path.join(directory, matchedFile)).isFile() !== clientFilePattern.isFile |
| 71 | + ) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + } |
| 76 | + } catch(e) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + return true; |
| 80 | +} |
| 81 | + |
| 82 | +function arrayFindPattern(array, pattern) { |
| 83 | + return array.find((item) => { |
| 84 | + const matched = new RegExp(pattern).exec(item); |
| 85 | + //RegExp.exec() returns null if no matched |
| 86 | + return Boolean(matched) |
| 87 | + }) |
| 88 | +} |
| 89 | + |
| 90 | +function generateClient(models, runtime) { |
| 91 | + const command = `node packages/package-generator/build/cli.js client --m ${models.service} ${models.smoke ? `--s ${models.smoke}` : ''} --r ${runtime}`; |
| 92 | + const packageRoot = path.dirname(__dirname); |
| 93 | + const log = execSync(command, {cwd: packageRoot}); |
| 94 | + console.info(log.toString()); |
| 95 | +} |
| 96 | + |
| 97 | +module.exports.clientNameRegex = clientNameRegex; |
0 commit comments