import { execSync } from 'child_process'; import { join, resolve } from 'path'; import { existsSync, readFileSync, writeFileSync } from 'fs'; import { error, warn, info, setDefaultLevel, LogLevelDesc } from 'loglevel'; setDefaultLevel(process.env.LOG_LEVEL as LogLevelDesc || 'INFO'); const loadJson = (file: string) => JSON.parse(readFileSync(file, { encoding: 'utf8' }).toString()); const awsBucketName: string | undefined = process.env.AWS_BUCKET; if (!awsBucketName) { error('AWS_BUCKET is not passed in environment variables'); process.exit(2); } if (!process.env.AWS_ACCESS_KEY_ID) { error('AWS_ACCESS_KEY_ID is not passed in environment variables'); process.exit(2); } if (!process.env.AWS_SECRET_ACCESS_KEY) { error('AWS_SECRET_ACCESS_KEY is not passed in environment variables'); process.exit(2); } const projectPath = process.argv[2]; if (!projectPath) { error('Project path is not passed in command line arguments'); process.exit(3); } const compodocConfigPath = join(projectPath, '.compodocrc.json'); if (!existsSync(compodocConfigPath)) { warn(`Project by path "${projectPath}" does not contain compodoc configuration file ".compodocrc.json". Skipping publish.`); process.exit(0); } const compodocConfigJson: { output: string; } = loadJson(compodocConfigPath); const docsPath = resolve(projectPath, compodocConfigJson.output); const packageJsonPath = join(projectPath, 'package.json'); const packageJson: { name: string; version: string, description: string, developerHub: { publishPath: string } } = loadJson(packageJsonPath); const { name: packageName, version: packageVersion, description: packageDescription, developerHub: { publishPath } } = packageJson; if (!packageName) { error(`Property "name" is not specified in ${packageJsonPath}`); process.exit(4); } if (!packageVersion) { error(`Property "version" is not specified in ${packageJsonPath}`); process.exit(4); } if (!packageDescription) { error(`Property "description" is not specified in ${packageJsonPath}`); process.exit(4); } if (!publishPath) { error(`Property "developerHub.publishPath" is not specified in ${packageJsonPath}`); process.exit(4); } const docsBasePath = join(__dirname, '..', 'docs'); const configTemplatePath = join(docsBasePath, 'config.template.yaml'); if (!existsSync(configTemplatePath)) { error(`"${configTemplatePath}" is not found`); process.exit(5); } const configTemplate = readFileSync(configTemplatePath, { encoding: 'utf8' }); const config = configTemplate .replace(/\$\{name\}/g, packageName) .replace(/\$\{version\}/g, packageVersion) .replace(/\$\{description\}/g, packageDescription); writeFileSync(join(docsPath, 'config.yaml'), config); const awsBucketPath = `${awsBucketName}/fe-docs/angular/libraries/${publishPath}/${packageVersion}/`; info(`Uploading docs from path '${docsPath}' to AWS S3 by path '${awsBucketPath}`); execSync(`aws s3 sync --acl=bucket-owner-full-control '${docsPath}' '${awsBucketPath}'`, { stdio: 'inherit', encoding: 'utf8', });