@aws-amplify/cli-internal
Version:
Amplify CLI
108 lines • 4.97 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fileOrDirectoryExists = exports.AmplifyYmlGenerator = void 0;
const node_path_1 = __importDefault(require("node:path"));
const promises_1 = __importDefault(require("node:fs/promises"));
const yaml = __importStar(require("yaml"));
const GEN1_COMMAND = '- amplifyPush\\b.*';
const GEN2_INSTALL_COMMAND = '- npm ci --cache .npm --prefer-offline';
const GEN2_COMMAND = '- npx ampx pipeline-deploy --branch $AWS_BRANCH --app-id $AWS_APP_ID';
const GEN2_REPLACE_STRING = `${GEN2_INSTALL_COMMAND}\n${' '.repeat(8)}${GEN2_COMMAND}`;
class AmplifyYmlGenerator {
constructor(gen1App) {
this.gen1App = gen1App;
}
async plan() {
const amplifyYmlPath = node_path_1.default.join(process.cwd(), 'amplify.yml');
const localFileExists = await fileOrDirectoryExists(amplifyYmlPath);
return [
{
validate: () => undefined,
describe: async () => [localFileExists ? 'Update amplify.yml with Gen2 build commands' : 'Generate amplify.yml'],
execute: async () => {
let parsed;
let fromExistingSource = false;
if (localFileExists) {
const existing = await promises_1.default.readFile(amplifyYmlPath, 'utf-8');
parsed = yaml.parse(existing);
fromExistingSource = true;
}
else {
const buildSpec = await this.gen1App.aws.fetchAppBuildSpec(this.gen1App.appId);
if (buildSpec) {
parsed = yaml.parse(buildSpec);
fromExistingSource = true;
}
}
if (!parsed) {
parsed = {
version: 1,
backend: {
phases: {
build: {
commands: [
'# Execute Amplify CLI with the helper script',
'npm ci --cache .npm --prefer-offline',
'npx ampx pipeline-deploy --branch $AWS_BRANCH --app-id $AWS_APP_ID',
],
},
},
},
frontend: {
phases: {
build: {
commands: ['mkdir dist', 'touch dist/index.html'],
},
},
artifacts: {
baseDirectory: 'dist',
files: ['**/*'],
},
},
};
}
let content = yaml.stringify(parsed);
if (fromExistingSource) {
content = content.replace(new RegExp(GEN1_COMMAND, 'g'), GEN2_REPLACE_STRING);
}
await promises_1.default.writeFile(amplifyYmlPath, content, 'utf-8');
},
},
];
}
}
exports.AmplifyYmlGenerator = AmplifyYmlGenerator;
async function fileOrDirectoryExists(targetPath) {
return promises_1.default
.access(targetPath)
.then(() => true)
.catch(() => false);
}
exports.fileOrDirectoryExists = fileOrDirectoryExists;
//# sourceMappingURL=amplify.yml.generator.js.map