feat(scaffolder): cookiecutter templater wip

Co-authored-by: Ben Lambert <ben@blam.sh>
This commit is contained in:
Ivan Shmidt
2020-06-23 11:56:56 +02:00
parent 602f06b2ff
commit bc15e7a710
8 changed files with 50 additions and 26 deletions
+1
View File
@@ -37,6 +37,7 @@
},
"devDependencies": {
"@backstage/cli": "^0.1.1-alpha.9",
"@types/dockerode": "^2.5.32",
"@types/fs-extra": "^9.0.1",
"@types/supertest": "^2.0.8",
"supertest": "^4.0.2",
@@ -1,12 +1,7 @@
#!/bin/bash
#!/bin/sh
# package name is "__component_id__" so that yarn doesn't throw an error
# about invalid characters when running yarn commands. here we replace it with the actual name
sed -i -e "s/__component_id__/{{ cookiecutter.component_id }}/g" package.json
# node_modules was moved out of the template folder, during the pre_gen hook,
# to avoid cookie_cutter from copying all of them. time to move it back
mv ../../node_modules.tmp ../../\{\{cookiecutter.component_id\}\}/node_modules 2>/dev/null ||:
# move back the build directory that was moved out in the pre_gen hook (if it exists)
mv ../../build.tmp ../../\{\{cookiecutter.component_id\}\}/build 2>/dev/null ||:
# Move all template files to the root folder
mv ./* ../
cd ..
rm -rf {{cookiecutter.component_id}}
# # # # # # # #
@@ -1,9 +0,0 @@
#!/bin/bash
# no way to ignore files in cookiecutter, so move node_modules out while building
# to avoid cookiecutter from copying all of them
mv ../../\{\{cookiecutter.component_id\}\}/node_modules ../../node_modules.tmp 2>/dev/null ||:
# cookicutter really doesn't like the next.js build directory, so if the app has
# been built from inside the template folder, that folders needs to be moved out as well
mv ../../\{\{cookiecutter.component_id\}\}/build ../../build.tmp 2>/dev/null ||:
@@ -1,5 +1,5 @@
{
"name": "__component_id__",
"name": "{{ cookiecutter.component_id }}",
"version": "0.0.0",
"description": "{{ cookiecutter.description }}",
"license": "UNLICENSED",
@@ -0,0 +1,11 @@
FROM alpine:3.7
RUN apk add --update \
git \
python \
python-dev \
py-pip \
g++ && \
pip install cookiecutter && \
apk del g++ py-pip python-dev && \
rm -rf /var/cache/apk/*
@@ -16,9 +16,18 @@ import { TemplaterBase, TemplaterRunOptions } from '.';
* limitations under the License.
*/
import fs from 'fs-extra';
import Docker from 'dockerode';
export class CookieCutter implements TemplaterBase {
private docker:Docker;
constructor() {
this.docker = new Docker();
}
public async run(options: TemplaterRunOptions): Promise<string> {
// first we need to make cookiecutter.json in the directory provided with the input values.
const cookieInfo = {
_copy_without_render: ['.github/workflows/*'],
@@ -26,7 +35,14 @@ export class CookieCutter implements TemplaterBase {
};
await fs.writeJSON(`${options.directory}/cookiecutter.json`, cookieInfo);
return '';
// run cookie cutter with new json
const realTemplatePath = await fs.promises.realpath(options.directory);
const outDir = realTemplatePath + '/result';
await this.docker.run('backstage/cookiecutter', ['cookiecutter', '--no-input', '-o', '/result', '/template', '--verbose'], process.stdout, {Volumes: { '/result': {}, '/template': {}}, HostConfig: {
Binds: [`${outDir}:/result`, `${realTemplatePath}:/template`],
}});
return outDir;
}
}
@@ -64,12 +64,15 @@ export async function createRouter(
const preparer = preparers.get(mockEntity);
// Run the preparer for the mock entity to produce a temporary directory with template in
const path = await preparer.prepare(mockEntity);
const skeletonPath = await preparer.prepare(mockEntity);
// Run the templater on the mock directory with values from the post body
await templater.run({ directory: path, values: { component_id: 'test' } });
const templatedPath = await templater.run({
directory: skeletonPath,
values: { component_id: 'test', description: "Something for now" },
});
console.warn(path);
console.warn(templatedPath);
});
const app = express();
+7
View File
@@ -3520,6 +3520,13 @@
resolved "https://registry.npmjs.org/@types/diff/-/diff-4.0.2.tgz#2e9bb89f9acc3ab0108f0f3dc4dbdcf2fff8a99c"
integrity sha512-mIenTfsIe586/yzsyfql69KRnA75S8SVXQbTLpDejRrjH0QSJcpu3AUOi/Vjnt9IOsXKxPhJfGpQUNMueIU1fQ==
"@types/dockerode@^2.5.32":
version "2.5.32"
resolved "https://registry.npmjs.org/@types/dockerode/-/dockerode-2.5.32.tgz#52d3628f605f8ea65202541c59a8a6dd166384fd"
integrity sha512-TfaGOoOHxsjkWRj2sPoQ3FLmTC5mVMhZ4kzZy13U7mjtIDoloE4e7AMj5jPLbffWB6Csy5DF5e0lC9M+tnKz/A==
dependencies:
"@types/node" "*"
"@types/eslint-visitor-keys@^1.0.0":
version "1.0.0"
resolved "https://registry.npmjs.org/@types/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#1ee30d79544ca84d68d4b3cdb0af4f205663dd2d"