-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(admin-ui-plugin): Add watch mode for UI extension development
Relates to #55
- Loading branch information
1 parent
bc321af
commit c0b4d3f
Showing
13 changed files
with
336 additions
and
93 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,4 @@ | ||
import path from 'path'; | ||
|
||
export const UI_PATH = path.join(__dirname, '../admin-ui'); | ||
export const loggerCtx = 'AdminUiPlugin'; |
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
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
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
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
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,80 @@ | ||
import { AdminUiExtension } from '@vendure/common/lib/shared-types'; | ||
import * as fs from 'fs-extra'; | ||
import * as path from 'path'; | ||
|
||
const EXTENSIONS_DIR = path.join(__dirname, '../src/app/extensions'); | ||
const EXTENSIONS_MODULES_DIR = 'modules'; | ||
const originalExtensionsModuleFile = path.join(EXTENSIONS_DIR, 'extensions.module.ts'); | ||
const tempExtensionsModuleFile = path.join(EXTENSIONS_DIR, 'extensions.module.ts.temp'); | ||
|
||
/** | ||
* Returns true if currently being executed from inside the Vendure monorepo. | ||
*/ | ||
export function isInVendureMonorepo(): boolean { | ||
return fs.existsSync(path.join(__dirname, '../../dev-server')); | ||
} | ||
|
||
/** | ||
* Restores the placeholder ExtensionsModule file from a template. | ||
*/ | ||
export function restoreExtensionsModule() { | ||
fs.copyFileSync(path.join(__dirname, 'extensions.module.ts.template'), originalExtensionsModuleFile); | ||
} | ||
|
||
/** | ||
* Deletes the contents of the /modules directory, which contains the plugin | ||
* extension modules copied over during the last compilation. | ||
*/ | ||
export function deleteExistingExtensionModules() { | ||
fs.removeSync(path.join(EXTENSIONS_DIR, EXTENSIONS_MODULES_DIR)); | ||
} | ||
|
||
/** | ||
* Copies all files from the ngModulePaths of the configured extensions into the | ||
* admin-ui source tree. | ||
*/ | ||
export function copyExtensionModules(extensions: Array<Required<AdminUiExtension>>) { | ||
for (const extension of extensions) { | ||
const dirName = path.basename(path.dirname(extension.ngModulePath)); | ||
const dest = getModuleOutputDir(extension); | ||
fs.copySync(extension.ngModulePath, dest); | ||
} | ||
} | ||
|
||
export function getModuleOutputDir(extension: Required<AdminUiExtension>): string { | ||
return path.join(EXTENSIONS_DIR, EXTENSIONS_MODULES_DIR, extension.id); | ||
} | ||
|
||
export function createExtensionsModule(extensions: Array<Required<AdminUiExtension>>) { | ||
const removeTsExtension = (filename: string): string => filename.replace(/\.ts$/, ''); | ||
const importPath = (e: Required<AdminUiExtension>): string => | ||
`./${EXTENSIONS_MODULES_DIR}/${e.id}/${removeTsExtension(e.ngModuleFileName)}`; | ||
fs.renameSync(originalExtensionsModuleFile, tempExtensionsModuleFile); | ||
|
||
const source = generateExtensionModuleTsSource( | ||
extensions.map(e => ({ className: e.ngModuleName, path: importPath(e) })), | ||
); | ||
fs.writeFileSync(path.join(EXTENSIONS_DIR, 'extensions.module.ts'), source, 'utf-8'); | ||
} | ||
|
||
export function restoreOriginalExtensionsModule() { | ||
fs.renameSync(originalExtensionsModuleFile, path.join(EXTENSIONS_DIR, 'extensions.module.ts.generated')); | ||
restoreExtensionsModule(); | ||
} | ||
|
||
function generateExtensionModuleTsSource(modules: Array<{ className: string; path: string }>): string { | ||
return `/** This file is generated by the build() function. Do not edit. */ | ||
import { CommonModule } from '@angular/common'; | ||
import { NgModule } from '@angular/core'; | ||
${modules.map(e => `import { ${e.className} } from '${e.path}';`).join('\n')} | ||
@NgModule({ | ||
imports: [ | ||
CommonModule, | ||
${modules.map(e => e.className + ',').join('\n')} | ||
], | ||
}) | ||
export class ExtensionsModule {} | ||
`; | ||
} |
Oops, something went wrong.