-
Notifications
You must be signed in to change notification settings - Fork 620
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli-plugin-scaffold-extensions): add link-extensions command
- Loading branch information
Showing
11 changed files
with
338 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const defaultConfig = require("../../.eslintrc"); | ||
|
||
module.exports = { | ||
...defaultConfig, | ||
rules: { | ||
...defaultConfig.rules, | ||
"import/dynamic-import-chunkname": 0 | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packages/cli-plugin-scaffold-extensions/src/generators/utils/generateAdminExtensions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
import { formatCode } from "@webiny/cli-plugin-scaffold/utils"; | ||
import { ExtensionWorkspace } from "./getExtensionsFromFilesystem"; | ||
|
||
export const generateAdminExtensions = async (extensions: ExtensionWorkspace[]) => { | ||
const extensionsFilePath = path.join("apps", "admin", "src", "Extensions.tsx"); | ||
|
||
const code: string[][] = []; | ||
|
||
extensions.forEach(extension => { | ||
const name = path.basename(extension.path); | ||
const ucFirstName = name.charAt(0).toUpperCase() + name.slice(1); | ||
const componentName = ucFirstName + "Extension"; | ||
const importStatement = `import { Extension as ${componentName}} from "${extension.packageJson.name}";`; | ||
code.push([importStatement, `<${componentName}/>`]); | ||
}); | ||
|
||
const extensionsFile = [ | ||
`// This file is automatically updated via scaffolding utilities.`, | ||
`import React from "react";`, | ||
...code.map(ext => ext[0]), | ||
"", | ||
`export const Extensions = () => { return (<>${code.map(ext => ext[1]).join("\n")}</>); };` | ||
]; | ||
|
||
fs.writeFileSync(extensionsFilePath, extensionsFile.join("\n")); | ||
|
||
await formatCode(extensionsFilePath, {}); | ||
}; |
29 changes: 29 additions & 0 deletions
29
packages/cli-plugin-scaffold-extensions/src/generators/utils/generateApiExtensions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
import { formatCode } from "@webiny/cli-plugin-scaffold/utils"; | ||
import { ExtensionWorkspace } from "./getExtensionsFromFilesystem"; | ||
|
||
export const generateApiExtensions = async (extensions: ExtensionWorkspace[]) => { | ||
const extensionsFilePath = path.join("apps", "api", "graphql", "src", "extensions.ts"); | ||
const code: string[][] = []; | ||
|
||
extensions.forEach(extension => { | ||
const name = path.basename(extension.path); | ||
const extensionFactory = name + "ExtensionFactory"; | ||
const importStatement = `import { createExtension as ${extensionFactory}} from "${extension.packageJson.name}";`; | ||
code.push([importStatement, `${extensionFactory}()`]); | ||
}); | ||
|
||
const extensionsFile = [ | ||
"// This file is automatically updated via scaffolding utilities.", | ||
...code.map(ext => ext[0]), | ||
"export const extensions = () => {", | ||
"return [", | ||
code.map(ext => ext[1]).join(","), | ||
"];};" | ||
]; | ||
|
||
fs.writeFileSync(extensionsFilePath, extensionsFile.join("\n")); | ||
|
||
await formatCode(extensionsFilePath, {}); | ||
}; |
33 changes: 33 additions & 0 deletions
33
packages/cli-plugin-scaffold-extensions/src/generators/utils/getExtensionsFromFilesystem.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import loadJson from "load-json-file"; | ||
import glob from "glob"; | ||
import path from "path"; | ||
import { PackageJson } from "@webiny/cli-plugin-scaffold/types"; | ||
|
||
export type ExtensionType = "api" | "admin" | undefined; | ||
|
||
export type ExtensionWorkspace = { | ||
path: string; | ||
type: ExtensionType; | ||
packageJson: PackageJson; | ||
}; | ||
|
||
export const getExtensionsFromFilesystem = (): ExtensionWorkspace[] => { | ||
const workspaces = glob.sync(`extensions/**/package.json`); | ||
return workspaces | ||
.map(pkg => ({ | ||
path: path.dirname(pkg), | ||
packageJson: loadJson.sync<PackageJson>(pkg) | ||
})) | ||
.map(workspace => { | ||
const typeKeyword = (workspace.packageJson.keywords || []).find(kw => { | ||
return kw.startsWith("webiny-extension-type:"); | ||
}); | ||
|
||
const type = (typeKeyword ? typeKeyword.split(":")[1] : undefined) as ExtensionType; | ||
|
||
return { | ||
...workspace, | ||
type | ||
}; | ||
}); | ||
}; |
16 changes: 16 additions & 0 deletions
16
packages/cli-plugin-scaffold-extensions/src/generators/utils/linkAllExtensions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { getExtensionsFromFilesystem } from "./getExtensionsFromFilesystem"; | ||
import { generateApiExtensions } from "./generateApiExtensions"; | ||
import { generateAdminExtensions } from "./generateAdminExtensions"; | ||
import { registerWorkspaces } from "./registerWorkspaces"; | ||
|
||
export const linkAllExtensions = async () => { | ||
const allExtensions = getExtensionsFromFilesystem(); | ||
const apiExtensions = allExtensions.filter(extension => extension.type === "api"); | ||
const adminExtensions = allExtensions.filter(extension => extension.type === "admin"); | ||
|
||
await Promise.all([ | ||
generateApiExtensions(apiExtensions), | ||
generateAdminExtensions(adminExtensions), | ||
registerWorkspaces(allExtensions) | ||
]); | ||
}; |
24 changes: 24 additions & 0 deletions
24
packages/cli-plugin-scaffold-extensions/src/generators/utils/registerWorkspaces.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import execa from "execa"; | ||
import loadJson from "load-json-file"; | ||
import writeJson from "write-json-file"; | ||
import type { ExtensionWorkspace } from "./getExtensionsFromFilesystem"; | ||
import { PackageJson } from "@webiny/cli-plugin-scaffold/types"; | ||
import { formatCode } from "@webiny/cli-plugin-scaffold/utils"; | ||
|
||
export const registerWorkspaces = async (extensions: ExtensionWorkspace[]) => { | ||
const packageJsonPath = "package.json"; | ||
const rootPkgJson = await loadJson<PackageJson>(packageJsonPath); | ||
|
||
extensions.forEach(extension => { | ||
const workspacePath = extension.path.replace(`${process.cwd()}/`, ""); | ||
|
||
if (!rootPkgJson.workspaces.packages.includes(workspacePath)) { | ||
rootPkgJson.workspaces.packages.push(workspacePath); | ||
} | ||
}); | ||
|
||
await writeJson(packageJsonPath, rootPkgJson); | ||
await formatCode(packageJsonPath, {}); | ||
|
||
await execa("yarn"); | ||
}; |
Oops, something went wrong.