|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | +import { initializeProject, availableInitTemplates } from '../../lib/init'; |
| 4 | +import { warning } from '../logging'; |
| 5 | +import * as nocti from '../vendor/noctilucent'; |
| 6 | + |
| 7 | +/** The list of languages supported by the built-in noctilucent binary. */ |
| 8 | +export const MIGRATE_SUPPORTED_LANGUAGES: readonly string[] = nocti.supported_languages(); |
| 9 | + |
| 10 | +export async function cliMigrate( |
| 11 | + inputpath: string = process.cwd() + '/../template.txt', |
| 12 | + language = MIGRATE_SUPPORTED_LANGUAGES[0], |
| 13 | + generateOnly = false, |
| 14 | + outputpath = process.cwd(), |
| 15 | +) { |
| 16 | + warning('This is an experimental feature. We make no guarantees about the outcome or stability of the functionality.'); |
| 17 | + const type = 'default'; // "default" is the default type (and maps to 'app') |
| 18 | + const template = (await availableInitTemplates()).find(t => t.hasName(type!)); |
| 19 | + if (!template) { |
| 20 | + throw new Error(`couldn't find template for ${type} app type, this should never happen`); |
| 21 | + } |
| 22 | + |
| 23 | + if (!MIGRATE_SUPPORTED_LANGUAGES.includes(language)) { |
| 24 | + throw new Error(`Unsupported language for cdk migrate: ${language}. Supported languages are: ${MIGRATE_SUPPORTED_LANGUAGES.join(', ')}`); |
| 25 | + } |
| 26 | + |
| 27 | + await initializeProject(template, language, true, generateOnly, outputpath); |
| 28 | + const template_file = fs.readFileSync(inputpath, 'utf8'); |
| 29 | + const generated_app = nocti.transmute(template_file, language); |
| 30 | + |
| 31 | + // clear out the init'd bin/lib files to replace with our own |
| 32 | + delete_files(outputpath + '/lib/'); |
| 33 | + |
| 34 | + // we hardcode everything to be called noctstack still so this works for now. |
| 35 | + // Will change this to be much smarter once we can change stack name in noct |
| 36 | + const bin_app = `#!/usr/bin/env node |
| 37 | + import 'source-map-support/register'; |
| 38 | + import * as cdk from 'aws-cdk-lib'; |
| 39 | + import { NoctStack } from '../lib/generated_stack'; |
| 40 | +
|
| 41 | + const app = new cdk.App(); |
| 42 | + new NoctStack(app, 'NoctStack', { |
| 43 | + /* If you don't specify 'env', this stack will be environment-agnostic. |
| 44 | + * Account/Region-dependent features and context lookups will not work, |
| 45 | + * but a single synthesized template can be deployed anywhere. */ |
| 46 | +
|
| 47 | + /* Uncomment the next line to specialize this stack for the AWS Account |
| 48 | + * and Region that are implied by the current CLI configuration. */ |
| 49 | + // env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION }, |
| 50 | +
|
| 51 | + /* Uncomment the next line if you know exactly what Account and Region you |
| 52 | + * want to deploy the stack to. */ |
| 53 | + // env: { account: '123456789012', region: 'us-east-1' }, |
| 54 | +
|
| 55 | + /* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */ |
| 56 | + });`; |
| 57 | + const myname = path.basename(outputpath); |
| 58 | + fs.writeFileSync(outputpath + '/lib/' + 'generated_stack.ts', generated_app); |
| 59 | + fs.writeFileSync(outputpath + '/bin/' + `${myname}.ts`, bin_app); |
| 60 | +} |
| 61 | + |
| 62 | +function delete_files(filepath: string) { |
| 63 | + fs.readdir(filepath, (err, files) => { |
| 64 | + if (err) throw err; |
| 65 | + for (const file of files) { |
| 66 | + fs.unlink(filepath + file, (cause) => { |
| 67 | + if (cause) throw cause; |
| 68 | + }); |
| 69 | + } |
| 70 | + }); |
| 71 | +} |
0 commit comments