cli/new: move package info and template globals to input resolution
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -26,7 +26,6 @@ import { executePluginPackageTemplate } from './executePluginPackageTemplate';
|
||||
import { TemporaryDirectoryManager } from './TemporaryDirectoryManager';
|
||||
import { PortableTemplateConfig, PortableTemplateInput } from '../types';
|
||||
import { PortableTemplate } from '../types';
|
||||
import { resolvePackageInfo } from './resolvePackageInfo';
|
||||
|
||||
type ExecuteNewTemplateOptions = {
|
||||
config: PortableTemplateConfig;
|
||||
@@ -37,13 +36,11 @@ type ExecuteNewTemplateOptions = {
|
||||
export async function executePortableTemplate(
|
||||
options: ExecuteNewTemplateOptions,
|
||||
) {
|
||||
const { config, template, input } = options;
|
||||
const { template, input } = options;
|
||||
|
||||
const tmpDirManager = TemporaryDirectoryManager.create();
|
||||
|
||||
const packageInfo = resolvePackageInfo(input);
|
||||
|
||||
const targetDir = paths.resolveTargetRoot(packageInfo.packagePath);
|
||||
const targetDir = paths.resolveTargetRoot(input.packageParams.packagePath);
|
||||
|
||||
let modified = false;
|
||||
try {
|
||||
@@ -59,13 +56,7 @@ export async function executePortableTemplate(
|
||||
targetDir,
|
||||
templateDir: template.templatePath,
|
||||
templateValues: template.templateValues,
|
||||
values: {
|
||||
packageName: packageInfo.packageName,
|
||||
privatePackage: input.globals.private,
|
||||
packageVersion: input.globals.baseVersion,
|
||||
license: input.globals.license,
|
||||
...input.params,
|
||||
},
|
||||
values: input.params,
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ import {
|
||||
PortableTemplateRole,
|
||||
} from '../types';
|
||||
import { PortableTemplate } from '../types';
|
||||
import { resolvePackageParams } from './resolvePackageParams';
|
||||
|
||||
const RESERVED_PROMPT_NAMES = ['name', 'pluginId', 'moduleId', 'owner'];
|
||||
|
||||
@@ -70,17 +71,28 @@ export async function collectPortableTemplateInput(
|
||||
...promptAnswers,
|
||||
};
|
||||
|
||||
const roleParams = {
|
||||
role: template.role,
|
||||
name: answers.name,
|
||||
pluginId: answers.pluginId,
|
||||
moduleId: answers.moduleId,
|
||||
} as PortableTemplateInputRoleParams;
|
||||
|
||||
const packageParams = resolvePackageParams(roleParams, config.globals);
|
||||
|
||||
return {
|
||||
roleParams: {
|
||||
role: template.role,
|
||||
name: answers.name,
|
||||
pluginId: answers.pluginId,
|
||||
moduleId: answers.moduleId,
|
||||
} as PortableTemplateInputRoleParams,
|
||||
roleParams,
|
||||
packageParams,
|
||||
builtInParams: {
|
||||
owner: answers.owner,
|
||||
} as PortableTemplateInputBuiltInParams,
|
||||
params: answers,
|
||||
params: {
|
||||
...answers,
|
||||
packageName: packageParams.packageName,
|
||||
privatePackage: config.globals.private,
|
||||
packageVersion: config.globals.baseVersion,
|
||||
license: config.globals.license,
|
||||
},
|
||||
globals: config.globals,
|
||||
};
|
||||
}
|
||||
|
||||
+2
-9
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import { resolvePackageInfo } from './resolvePackageInfo';
|
||||
import { resolvePackageParams } from './resolvePackageParams';
|
||||
|
||||
const baseGlobals = {
|
||||
baseVersion: '0.0.0',
|
||||
@@ -96,13 +96,6 @@ describe.each([
|
||||
],
|
||||
] as const)('resolvePackageInfo', (roleParams, packageInfo) => {
|
||||
it(`should generate correct info with default config for ${roleParams.role}`, () => {
|
||||
expect(
|
||||
resolvePackageInfo({
|
||||
builtInParams: {},
|
||||
roleParams,
|
||||
params: {},
|
||||
globals: baseGlobals,
|
||||
}),
|
||||
).toEqual(packageInfo);
|
||||
expect(resolvePackageParams(roleParams, baseGlobals)).toEqual(packageInfo);
|
||||
});
|
||||
});
|
||||
+8
-7
@@ -16,7 +16,7 @@
|
||||
|
||||
import { join as joinPath } from 'path';
|
||||
import {
|
||||
PortableTemplateInput,
|
||||
PortableTemplateGlobals,
|
||||
PortableTemplateInputRoleParams,
|
||||
} from '../types';
|
||||
|
||||
@@ -25,14 +25,15 @@ export type PortableTemplatePackageInfo = {
|
||||
packagePath: string;
|
||||
};
|
||||
|
||||
export function resolvePackageInfo(
|
||||
input: PortableTemplateInput,
|
||||
export function resolvePackageParams(
|
||||
roleParams: PortableTemplateInputRoleParams,
|
||||
globals: PortableTemplateGlobals,
|
||||
): PortableTemplatePackageInfo {
|
||||
const baseName = getBaseNameForRole(input.roleParams);
|
||||
const isPlugin = input.roleParams.role.includes('plugin');
|
||||
const pluginInfix = isPlugin ? input.globals.pluginInfix : '';
|
||||
const baseName = getBaseNameForRole(roleParams);
|
||||
const isPlugin = roleParams.role.includes('plugin');
|
||||
const pluginInfix = isPlugin ? globals.pluginInfix : '';
|
||||
return {
|
||||
packageName: `${input.globals.packagePrefix}${pluginInfix}${baseName}`,
|
||||
packageName: `${globals.packagePrefix}${pluginInfix}${baseName}`,
|
||||
packagePath: joinPath(isPlugin ? 'plugins' : 'packages', baseName),
|
||||
};
|
||||
}
|
||||
@@ -105,9 +105,15 @@ export type PortableTemplateInputBuiltInParams = {
|
||||
owner?: string;
|
||||
};
|
||||
|
||||
export type PortableTemplateInputPackageParams = {
|
||||
packageName: string;
|
||||
packagePath: string;
|
||||
};
|
||||
|
||||
export type PortableTemplateInput = {
|
||||
roleParams: PortableTemplateInputRoleParams;
|
||||
builtInParams: PortableTemplateInputBuiltInParams;
|
||||
packageParams: PortableTemplateInputPackageParams;
|
||||
params: PortableTemplateParams;
|
||||
globals: PortableTemplateGlobals;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user