Skip to content

Commit

Permalink
Template is saved in /packages instead of /api. The generated files h…
Browse files Browse the repository at this point in the history
…ave been wrapped inside /src/plugins. Package.json's name is updated according to {serviceName}. [WIP #1] The plugins used in src/plugins/index.ts are almost included in serverless.yml. [WIP #2] Yarn is run after the template is generated.
  • Loading branch information
Fsalker committed Mar 9, 2020
1 parent 3b4c5e1 commit 235ad5a
Show file tree
Hide file tree
Showing 15 changed files with 241 additions and 25 deletions.
33 changes: 26 additions & 7 deletions packages/cli-scaffold-graphql-service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ const util = require("util");
const ncp = util.promisify(require("ncp").ncp);
const execa = require("execa");

const getServiceLocation = ({ context, serviceName }) => path.join(context.apiPath, serviceName);
// path.join(context.packagesPath, `api-${serviceName}`);
const getServiceLocation = (
{ context, serviceName } //path.join(context.apiPath, serviceName);
) => path.join(context.packagesPath, `api-${serviceName}`);

module.exports = [
{
name: "scaffold-template-apollo-service",
type: "scaffold-template",
scaffold: {
name: "GraphQL Apollo Service",
// description: "Creates an /api template and fills it in serverless.yml",
questions: ({ context }) => {
if (!fs.existsSync(context.apiYaml)) {
console.log(
Expand All @@ -40,7 +40,6 @@ module.exports = [
},
generate: async ({ input, context }) => {
const warnings = [];
await new Promise(res => setTimeout(() => res(), 2000));
try {
// First we update serverless.yml
const { serviceName } = input;
Expand Down Expand Up @@ -86,6 +85,17 @@ module.exports = [
}
]);

// [WIP #1] These two are the plugins defined in /src/plugins/graphql.ts and /src/plugins/models.ts respectively
// How do we acutally include them in the 'plugins' array in serverlessJson below?

// const templateServicePlugins = [
// {
// name: "graphql-schema-thingy"
// },
// {
// name: "graphql-context-models"
// }
// ];
serverlessJson[serviceName] = {
component: "@webiny/serverless-apollo-service",
inputs: {
Expand Down Expand Up @@ -115,11 +125,20 @@ module.exports = [
throw new Error(
`Service ${serviceName} already exists! This error should've been thrown earlier...`
);
await fs.mkdirSync(destFolder);
await fs.mkdirSync(destFolder, { recursive: true });
await ncp(sourceFolder, destFolder);

// [WIP] Run "yarn build" in order to link the new package
// await execa("yarn", ["--cwd", context.apiPath]);
// Update the package's name
const servicePackageJsonPath = path.join(serviceLocation, "package.json");
let servicePackageJson = fs.readFileSync(servicePackageJsonPath).toString();
servicePackageJson = servicePackageJson.replace("SERVICE_NAME", serviceName);
fs.writeFileSync(servicePackageJsonPath, servicePackageJson);

// [WIP #2] Does this guy work? Well, I've attempted to test it locally but it seems to break the
// installed packages; testing it with a freshly created webiny app is troublesome aswell, as
// 'yarn list' doesn't show the GraphQL Service Template ;/

// await execa("yarn");
} catch (e) {
console.log(e);
} finally {
Expand Down
141 changes: 141 additions & 0 deletions packages/cli-scaffold-graphql-service/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions packages/cli-scaffold-graphql-service/templateService/.babelrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const alias = require("@webiny/project-utils/aliases/jest");

const babel = {
presets: [
[
"@babel/preset-env",
{
targets: {
node: "8.10"
}
}
],
"@babel/preset-typescript"
],
plugins: [
["@babel/plugin-proposal-class-properties"],
["@babel/plugin-proposal-object-rest-spread", { useBuiltIns: true }],
["@babel/plugin-transform-runtime", { useESModules: false }],
["babel-plugin-dynamic-import-node"],
["babel-plugin-lodash"],
process.env.NODE_ENV === "test" ? ["babel-plugin-module-resolver", { alias }] : null
].filter(Boolean)
};

module.exports = babel;
//
//
// module.exports = require("../../.babel.node");
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "@webiny/api-[SERVICE_NAME]",
"name": "@webiny/api-SERVICE_NAME",
"version": "1.0.0",
"description": "",
"main": "./src/index.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"build": "rimraf ./dist *.tsbuildinfo && babel src -d dist --source-maps --copy-files --extensions \".ts\"",
"watch": "babel src -d dist --source-maps --copy-files --extensions \".ts\" --watch"
},
"keywords": [],
"author": "",
Expand Down
14 changes: 0 additions & 14 deletions packages/cli-scaffold-graphql-service/templateService/src/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { GraphQLSchemaPlugin } from "@webiny/api/types";

const plugin: GraphQLSchemaPlugin = {
type: "graphql-schema",
name: "graphql-schema-i18n",
name: "graphql-schema-thingy",
schema: {
typeDefs: gql`
${graphqlSchema.typeDefs}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import models from "./models";
import graphql from "./graphql";

export default () => [
models(),
graphql
/* ... Other plugins to be exported ... */
];
Loading

0 comments on commit 235ad5a

Please sign in to comment.