@cardstack/cli
Version:
Command line tools for Cardstack.
168 lines (165 loc) • 6.13 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const crypto_1 = require("crypto");
const path_1 = require("path");
const os_1 = require("os");
const hash_for_dep_1 = __importDefault(require("hash-for-dep"));
const typescript_memoize_1 = require("typescript-memoize");
const fs_extra_1 = require("fs-extra");
const rewriters_1 = require("./rewriters");
const exec_1 = __importDefault(require("./utils/exec"));
const appName = "@cardstack/cardhost";
const ephemeralConfig = `
module.exports = [
{
type: 'data-sources',
id: 'default',
attributes: {
'source-type': '@cardstack/ephemeral'
}
},
{
type: 'plugin-configs',
id: '@cardstack/hub',
relationships: {
'default-data-source': {
data: { type: 'data-sources', id: 'default' }
}
}
}
];
`;
const optionalFeatures = `
{
"application-template-wrapper": false,
"jquery-integration": true,
"template-only-glimmer-components": true
}
`;
const routerJS = `
import EmberRouter from "@ember/routing/router";
import config from "./config/environment";
import { cardstackRoutes } from '@cardstack/routing';
const Router = EmberRouter.extend({
location: config.locationType,
rootURL: config.rootURL
});
Router.map(cardstackRoutes);
export default Router;
`;
const cardstackDeps = ["hub", "jsonapi", "ephemeral"];
const otherDeps = [
"@embroider/core",
"@embroider/compat",
"@embroider/webpack",
"@ember/jquery"
];
async function preBuild(options) {
let builder = new PreBuilder(options);
return builder.preBuild();
}
exports.default = preBuild;
class PreBuilder {
constructor({ dir, ui }) {
this.destDir = dir;
this.ui = ui;
fs_extra_1.ensureDirSync(this.workDir);
ui.writeInfoLine(`using temp dir ${this.workDir}`);
ui.writeInfoLine(`destination is ${this.destDir}/blueprints`);
}
get workDir() {
let hash = crypto_1.createHash("md5");
hash.update(this.destDir +
"\0" +
(process.env.CARDSTACK_DEV ? "nocache" : hash_for_dep_1.default(__dirname)));
return path_1.join(os_1.tmpdir(), "cardstack", hash.digest("hex").slice(0, 6));
}
get appDir() {
return path_1.join(this.workDir, appName);
}
exclude(filepath) {
if (filepath.includes("node_modules")) {
return false;
}
else if (filepath.includes(".cardstack-ready")) {
return false;
}
else {
return true;
}
}
copyToBlueprint() {
let blueprintsDir = path_1.join(this.destDir, "blueprints");
fs_extra_1.ensureDirSync(blueprintsDir);
fs_extra_1.copySync(this.appDir, blueprintsDir, { filter: this.exclude });
this.ui.writeInfoLine(`Blueprints are now ready for review in ${this.destDir}/blueprints`);
}
async preBuild() {
this.createFiles();
}
// generate all app files in a tmp directory, and copy them into the cwd/blueprints on success
createFiles() {
let readyMarker = path_1.join(this.appDir, ".cardstack-ready");
if (!fs_extra_1.pathExistsSync(readyMarker)) {
fs_extra_1.removeSync(this.appDir);
this.generateEmberApp();
this.installDependencies();
this.enhanceApp();
fs_extra_1.writeFileSync(readyMarker, "");
this.copyToBlueprint();
}
}
generateEmberApp() {
let ember = require.resolve("ember-cli/bin/ember");
let blueprint = path_1.dirname(require.resolve("@ember/octane-app-blueprint/package.json"));
exec_1.default(ember, [
"new",
appName,
"--blueprint",
blueprint,
"--skip-npm",
"--skip-git",
"--welcome",
"false"
], { cwd: this.workDir }, this.ui);
}
installDependencies() {
let yarn = require.resolve("yarn/bin/yarn");
exec_1.default(yarn, [
"add",
"--dev",
...cardstackDeps.map(d => `/${d}`),
...otherDeps
], {
cwd: this.appDir
}, this.ui);
}
enhanceApp() {
fs_extra_1.mkdirpSync(path_1.join(this.appDir, "cardstack", "data-sources"));
fs_extra_1.writeFileSync(path_1.join(this.appDir, "cardstack", "data-sources", "ephemeral.js"), ephemeralConfig);
fs_extra_1.writeFileSync(path_1.join(this.appDir, "app", "router.js"), routerJS);
// workaround for https://github.com/ember-cli/ember-octane-blueprint/issues/100
fs_extra_1.writeFileSync(path_1.join(this.appDir, "app", "templates", "application.hbs"), "{{#cardstack-edges}}{{outlet}}{{/cardstack-edges}}");
let buildFilePath = path_1.join(this.appDir, "ember-cli-build.js");
fs_extra_1.writeFileSync(buildFilePath, rewriters_1.rewriteEmberCLIBuild(fs_extra_1.readFileSync(buildFilePath, "utf8")));
fs_extra_1.mkdirpSync(path_1.join(this.appDir, "config"));
let optionalConfigPath = path_1.join(this.appDir, "config", "optional-features.json");
fs_extra_1.writeFileSync(optionalConfigPath, optionalFeatures);
}
}
__decorate([
typescript_memoize_1.Memoize()
], PreBuilder.prototype, "workDir", null);
__decorate([
typescript_memoize_1.Memoize()
], PreBuilder.prototype, "appDir", null);
//# sourceMappingURL=pre-build.js.map