Fix CRA templating

The GitHub actions setup was never templated correctly.

Use known directories for templating
This commit is contained in:
Johan Haals
2021-01-28 15:34:35 +01:00
parent cdea0baf1c
commit 327c94da39
2 changed files with 74 additions and 69 deletions
@@ -17,25 +17,30 @@ import fs from 'fs-extra';
import { runDockerContainer } from '../helpers';
import { TemplaterBase, TemplaterRunOptions } from '..';
import path from 'path';
import { TemplaterRunResult } from '../types';
import * as yaml from 'yaml';
import { resolvePackagePath } from '@backstage/backend-common';
// TODO(blam): Replace with the universal import from github-actions after a release
// As it will break the E2E without it
const GITHUB_ACTIONS_ANNOTATION = 'github.com/project-slug';
export class CreateReactAppTemplater implements TemplaterBase {
public async run(options: TemplaterRunOptions): Promise<TemplaterRunResult> {
public async run({
workspacePath,
values,
logStream,
dockerClient,
}: TemplaterRunOptions): Promise<void> {
const {
component_id: componentName,
use_typescript: withTypescript,
use_github_actions: withGithubActions,
description,
owner,
} = options.values;
} = values;
const intermediateDir = path.join(workspacePath, 'template');
await fs.ensureDir(intermediateDir);
const resultDir = await fs.promises.mkdtemp(`${options.directory}-result`);
const resultDir = path.join(workspacePath, 'result');
await runDockerContainer({
imageName: 'node:lts-alpine',
@@ -44,35 +49,80 @@ export class CreateReactAppTemplater implements TemplaterBase {
componentName as string,
withTypescript ? ' --template typescript' : '',
],
templateDir: options.directory,
resultDir,
logStream: options.logStream,
dockerClient: options.dockerClient,
templateDir: intermediateDir,
resultDir: intermediateDir,
logStream: logStream,
dockerClient: dockerClient,
createOptions: {
Entrypoint: ['npx'],
WorkingDir: '/result',
},
});
const extraAnnotations: Record<string, string> = {};
const finalDir = path.resolve(
resultDir,
options.values.component_id as string,
);
// if cookiecutter was successful, intermediateDir will contain
// exactly one directory.
const [generated] = await fs.readdir(intermediateDir);
if (generated === undefined) {
throw new Error('No data generated by cookiecutter');
}
await fs.move(path.join(intermediateDir, generated), resultDir);
const extraAnnotations: Record<string, string> = {};
if (withGithubActions) {
await fs.promises.mkdir(`${finalDir}/.github`);
await fs.promises.mkdir(`${finalDir}/.github/workflows`);
await fs.promises.copyFile(
`${resolvePackagePath(
'@backstage/plugin-scaffolder-backend',
)}/templates/.github/workflows/main.yml`,
`${finalDir}/.github/workflows/main.yml`,
await fs.mkdir(`${resultDir}/.github`);
await fs.mkdir(`${resultDir}/.github/workflows`);
const githubActionsYaml = `
name: CRA Build
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x]
steps:
- name: checkout code
uses: actions/checkout@v1
- name: get yarn cache
id: yarn-cache
run: echo "::set-output name=dir::$(yarn cache dir)"
- uses: actions/cache@v2
with:
path: \${{ steps.yarn-cache.outputs.dir }}
key: \${{ runner.os }}-yarn-\${{ hashFiles('**/yarn.lock') }}
restore-keys: |
\${{ runner.os }}-yarn-
- name: use node.js \${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: \${{ matrix.node-version }}
- name: yarn install, build, and test
working-directory: .
run: |
yarn install
yarn build
yarn test
env:
CI: true
`;
await fs.writeFile(
`${resultDir}/.github/workflows/main.yml`,
githubActionsYaml,
);
extraAnnotations[
GITHUB_ACTIONS_ANNOTATION
] = `${options.values?.destination?.git?.owner}/${options.values?.destination?.git?.name}`;
] = `${values?.destination?.git?.owner}/${values?.destination?.git?.name}`;
}
const componentInfo = {
@@ -92,13 +142,9 @@ export class CreateReactAppTemplater implements TemplaterBase {
},
};
await fs.promises.writeFile(
`${finalDir}/catalog-info.yaml`,
await fs.writeFile(
`${resultDir}/catalog-info.yaml`,
yaml.stringify(componentInfo),
);
return {
resultDir: finalDir,
};
}
}
@@ -1,41 +0,0 @@
name: CRA Build
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x]
steps:
- name: checkout code
uses: actions/checkout@v1
- name: get yarn cache
id: yarn-cache
run: echo "::set-output name=dir::$(yarn cache dir)"
- uses: actions/cache@v2
with:
path: ${{ steps.yarn-cache.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: use node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: yarn install, build, and test
working-directory: .
run: |
yarn install
yarn build
yarn test
env:
CI: true