Skip to content

Commit

Permalink
chore: more mock build refactoring and types
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardfoyle committed Jan 27, 2021
1 parent 33a1a02 commit c8a3c55
Show file tree
Hide file tree
Showing 19 changed files with 152 additions and 102 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { $TSContext } from 'amplify-cli-core';
import { ServiceName } from '../..';
import { category } from '../../constants';
import { ResourceMeta } from '../../provider-utils/awscloudformation/types/packaging-types';
import { PackageRequestMeta } from '../../provider-utils/awscloudformation/types/packaging-types';
import { buildFunction } from '../../provider-utils/awscloudformation/utils/buildFunction';
import { packageResource } from '../../provider-utils/awscloudformation/utils/package';

Expand Down Expand Up @@ -39,5 +39,5 @@ export const run = async (context: $TSContext) => {
};

const getSelectedResources = async (context: $TSContext, resourceName?: string) => {
return (await context.amplify.getResourceStatus(category, resourceName)).allResources as ResourceMeta[];
return (await context.amplify.getResourceStatus(category, resourceName)).allResources as PackageRequestMeta[];
};
13 changes: 11 additions & 2 deletions packages/amplify-category-function/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import path from 'path';
import { category } from './constants';
export { category } from './constants';
import { FunctionBreadcrumbs, FunctionRuntimeLifecycleManager } from 'amplify-function-plugin-interface';
import { BuildType, FunctionBreadcrumbs, FunctionRuntimeLifecycleManager } from 'amplify-function-plugin-interface';
import { $TSContext, pathManager, stateManager } from 'amplify-cli-core';
import sequential from 'promise-sequential';
import { updateConfigOnEnvInit } from './provider-utils/awscloudformation';
import { supportedServices } from './provider-utils/supported-services';
import _ from 'lodash';
export { buildFunction } from './provider-utils/awscloudformation/utils/buildFunction';
export { buildFunction, buildTypeKeyMap } from './provider-utils/awscloudformation/utils/buildFunction';
export { packageResource } from './provider-utils/awscloudformation/utils/package';
export { hashLayerResource } from './provider-utils/awscloudformation/utils/packageLayer';
import { ServiceName } from './provider-utils/awscloudformation/utils/constants';
export { ServiceName } from './provider-utils/awscloudformation/utils/constants';
import { isMultiEnvLayer } from './provider-utils/awscloudformation/utils/layerParams';
import { buildFunction, buildTypeKeyMap } from './provider-utils/awscloudformation/utils/buildFunction';
import { PackageRequestMeta } from './provider-utils/awscloudformation/types/packaging-types';
export { isMultiEnvLayer } from './provider-utils/awscloudformation/utils/layerParams';

export { askExecRolePermissionsQuestions } from './provider-utils/awscloudformation/service-walkthroughs/execPermissionsWalkthrough';
Expand Down Expand Up @@ -169,6 +171,13 @@ export async function getInvoker(
});
}

export function getBuilder(context: $TSContext, resourceName: string, buildType: BuildType): () => Promise<void> {
const lastBuildTimeStamp = _.get(stateManager.getMeta(), [category, resourceName, buildTypeKeyMap[buildType]]);
return async () => {
await buildFunction(context, { resourceName, buildType, lastBuildTimeStamp });
};
}

export function isMockable(context: any, resourceName: string): IsMockableResponse {
const resourceValue = _.get(context.amplify.getProjectMeta(), [category, resourceName]);
if (!resourceValue) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { $TSContext, ResourceTuple } from 'amplify-cli-core';

export type ResourceMeta = ResourceTuple & {
export type PackageRequestMeta = ResourceTuple & {
service: string;
build: boolean;
distZipFilename: string;
Expand All @@ -9,4 +9,4 @@ export type ResourceMeta = ResourceTuple & {
skipHashing: boolean;
};

export type Packager = (context: $TSContext, resource: ResourceMeta) => Promise<{ zipFilename: string; zipFilePath: string }>;
export type Packager = (context: $TSContext, resource: PackageRequestMeta) => Promise<{ zipFilename: string; zipFilePath: string }>;
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { $TSContext, pathManager } from 'amplify-cli-core';
import { FunctionRuntimeLifecycleManager, BuildRequest, BuildType } from 'amplify-function-plugin-interface';
import { ResourceMeta } from '../types/packaging-types';
import * as path from 'path';
import { category } from '../../../constants';

export const buildFunction = async (context: $TSContext, resource: ResourceMeta, buildType: BuildType = BuildType.PROD) => {
const resourcePath = path.join(pathManager.getBackendDirPath(), resource.category, resource.resourceName);
const breadcrumbs = context.amplify.readBreadcrumbs(resource.category, resource.resourceName);
export const buildFunction = async (
context: $TSContext,
{ resourceName, lastBuildTimeStamp, buildType = BuildType.PROD }: BuildRequestMeta,
) => {
const resourcePath = path.join(pathManager.getBackendDirPath(), category, resourceName);
const breadcrumbs = context.amplify.readBreadcrumbs(category, resourceName);

const runtimePlugin: FunctionRuntimeLifecycleManager = (await context.amplify.loadRuntimePlugin(
context,
Expand All @@ -14,11 +17,11 @@ export const buildFunction = async (context: $TSContext, resource: ResourceMeta,

const depCheck = await runtimePlugin.checkDependencies(breadcrumbs.functionRuntime);
if (!depCheck.hasRequiredDependencies) {
context.print.error(depCheck.errorMessage || `You are missing dependencies required to package ${resource.resourceName}`);
throw new Error(`Missing required dependencies to package ${resource.resourceName}`);
context.print.error(depCheck.errorMessage || `You are missing dependencies required to package ${resourceName}`);
throw new Error(`Missing required dependencies to package ${resourceName}`);
}

const prevBuildTime = resource.lastBuildTimeStamp ? new Date(resource.lastBuildTimeStamp) : undefined;
const prevBuildTime = lastBuildTimeStamp ? new Date(lastBuildTimeStamp) : undefined;

// build the function
let rebuilt = false;
Expand All @@ -32,16 +35,27 @@ export const buildFunction = async (context: $TSContext, resource: ResourceMeta,
runtime: breadcrumbs.functionRuntime,
legacyBuildHookParams: {
projectRoot: pathManager.findProjectRoot(),
resourceName: resource.resourceName,
resourceName: resourceName,
},
lastBuildTimeStamp: prevBuildTime,
};
rebuilt = (await runtimePlugin.build(buildRequest)).rebuilt;
}
if (rebuilt) {
context.amplify.updateamplifyMetaAfterBuild(resource, buildType);
context.amplify.updateamplifyMetaAfterBuild({ category, resourceName }, buildType.toString());
return new Date().toISOString();
} else {
return resource?.lastBuildTimeStamp;
return lastBuildTimeStamp;
}
};

export interface BuildRequestMeta {
resourceName: string;
lastBuildTimeStamp?: string;
buildType?: BuildType;
}

export const buildTypeKeyMap: Record<BuildType, string> = {
[BuildType.PROD]: 'lastBuildTimeStamp',
[BuildType.DEV]: 'lastDevBuildTimeStamp',
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ import {
FunctionRuntimeCondition,
FunctionRuntimeParameters,
FunctionTemplateParameters,
Contributor,
FunctionRuntimeLifecycleManager,
RuntimeContributionRequest,
TemplateContributionRequest,
} from 'amplify-function-plugin-interface';
import { ServiceName } from './constants';
import _ from 'lodash';
import { LayerParameters } from './layerParams';
import { ResourceMeta } from '../types/packaging-types';
import { $TSContext, ResourceTuple } from 'amplify-cli-core';
import { $TSContext } from 'amplify-cli-core';
import { category } from '../../../constants';
/*
* This file contains the logic for loading, selecting and executing function plugins (currently runtime and template plugins)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { $TSContext } from 'amplify-cli-core';
import _ from 'lodash';
import { Packager, ResourceMeta } from '../types/packaging-types';
import { Packager } from '../types/packaging-types';
import { ServiceName } from './constants';
import { packageFunction } from './packageFunction';
import { packageLayer } from './packageLayer';

export const packageResource: Packager = async (context, resource) => getServicePackager(resource.service)(context, resource);

const getServicePackager = (service: string) => (servicePackagerMap[service] ?? packageUnknown) as Packager;

const servicePackagerMap: Record<ServiceName, Packager> = {
[ServiceName.LambdaFunction]: packageFunction,
[ServiceName.LambdaLayer]: packageLayer,
};

const getServicePackager = (service: string) =>
(servicePackagerMap[service] ??
(() => {
throw new Error(`Unknown function service type ${service}`);
})) as Packager;
const packageUnknown: Packager = (_, resource) => {
throw new Error(`Unknown function service type ${resource.service}`);
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import { getLayerRuntimes } from './layerRuntimes';
import crypto from 'crypto';
import { updateLayerArtifacts } from './storeResources';
import globby from 'globby';
import { Packager, ResourceMeta } from '../types/packaging-types';
import { Packager, PackageRequestMeta } from '../types/packaging-types';

export const packageLayer: Packager = async (context, resource) => {
await ensureLayerVersion(context, resource.resourceName);
return zipLayer(context, resource);
};

async function zipLayer(context: $TSContext, resource: ResourceMeta) {
async function zipLayer(context: $TSContext, resource: PackageRequestMeta) {
const zipFilename = 'latest-build.zip';
const layerName = resource.resourceName;
const layerDirPath = path.join(pathManager.getBackendDirPath(), resource.category, layerName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function createFunctionResources(context: $TSContext, parameters: Functio
copyTemplateFiles(context, parameters);
saveMutableState(parameters);
saveCFNParameters(parameters);
context.amplify.leaveBreadcrumbs(context, categoryName, parameters.resourceName, createBreadcrumbs(parameters));
context.amplify.leaveBreadcrumbs(categoryName, parameters.resourceName, createBreadcrumbs(parameters));
}

export const createLayerArtifacts = (context: $TSContext, parameters: LayerParameters, latestVersion: number = 1): string => {
Expand Down
2 changes: 1 addition & 1 deletion packages/amplify-cli-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"@types/node": "^10.17.13",
"@types/rimraf": "^3.0.0",
"@types/uuid": "^8.0.0",
"amplify-function-plugin-interface": "1.4.1",
"amplify-function-plugin-interface": "1.6.0",
"rimraf": "^3.0.0"
},
"jest": {
Expand Down
23 changes: 20 additions & 3 deletions packages/amplify-cli-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ServiceSelection } from './serviceSelection';
import { BuildType } from 'amplify-function-plugin-interface';

export * from './cliContext';
export * from './cliContextEnvironmentProvider';
Expand Down Expand Up @@ -36,6 +35,21 @@ export type $TSContext = {
runtime: $TSAny;
pluginPlatform: IPluginPlatform;
newUserInfo?: $TSAny;
filesystem: IContextFilesystem;
template: IContextTemplate;
};

export type IContextFilesystem = {
remove: (targetPath: string) => void;
read: (targetPath: string, encoding?: string) => $TSAny;
write: (targetPath: string, data: unknown) => void;
exists: (targetPath: string) => boolean;
isFile: (targetPath: string) => boolean;
path: (...pathParts: string[]) => string;
};

export type IContextTemplate = {
generate: (opts: { template: string; target: string; props: object; directory: string }) => string;
};

export type IPluginPlatform = {
Expand Down Expand Up @@ -125,12 +139,14 @@ export interface AmplifyProjectConfig {
providers: string[];
}

export type $TSCopyJob = any;

// Temporary interface until Context refactor
interface AmplifyToolkit {
confirmPrompt: (prompt: string, defaultValue?: boolean) => boolean;
constants: $TSAny;
constructExeInfo: (context: $TSContext) => $TSAny;
copyBatch: (context: $TSContext, jobs: $TSAny, props: $TSAny, force: boolean, writeParams?: $TSAny[] | $TSObject) => Promise<void>;
copyBatch: (context: $TSContext, jobs: $TSCopyJob[], props: object, force?: boolean, writeParams?: boolean | object) => $TSAny;
crudFlow: () => $TSAny;
deleteProject: () => $TSAny;
executeProviderUtils: () => $TSAny;
Expand Down Expand Up @@ -196,7 +212,8 @@ interface AmplifyToolkit {
updateamplifyMetaAfterResourceDelete: (category: string, resourceName: string) => void;
updateProvideramplifyMeta: (providerName: string, options: $TSObject) => void;
updateamplifyMetaAfterPush: (resources: $TSObject[]) => void;
updateamplifyMetaAfterBuild: (resource: ResourceTuple, buildType?: BuildType) => void;
// buildType is from amplify-function-plugin-interface but can't be imported here because it would create a circular dependency
updateamplifyMetaAfterBuild: (resource: ResourceTuple, buildType?: string) => void;
updateAmplifyMetaAfterPackage: (resource: ResourceTuple, zipFilename: string) => void;
updateBackendConfigAfterResourceAdd: (category: string, resourceName: string, resourceData: $TSAny) => $TSAny;
updateBackendConfigAfterResourceUpdate: () => $TSAny;
Expand Down
2 changes: 1 addition & 1 deletion packages/amplify-cli/bin/amplify
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env node --inspect-brk
#!/usr/bin/env node
if (process.pkg) {
require('../lib/utils/copy-override').copyOverride();
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/amplify-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
"@types/promise-sequential": "^1.1.0",
"@types/tar-fs": "^2.0.0",
"@types/update-notifier": "^4.1.0",
"amplify-function-plugin-interface": "1.4.1",
"amplify-function-plugin-interface": "1.6.0",
"nock": "^12.0.3"
},
"jest": {
Expand Down
2 changes: 1 addition & 1 deletion packages/amplify-cli/src/context-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ const CLI_TABLE_MARKDOWN = {

function attachTemplate(context: Context) {
context.template = {
async generate(opts: any): Promise<string> {
async generate(opts: { template: string; target: string; props: object; directory: string }): Promise<string> {
const ejs = require('ejs');
const template = opts.template;
const target = opts.target;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JSONUtilities, $TSAny, $TSContext, $TSObject } from 'amplify-cli-core';
import { $TSContext, $TSCopyJob, JSONUtilities } from 'amplify-cli-core';

/**
* Runs a series of jobs through the templating system.
Expand All @@ -7,15 +7,15 @@ import { JSONUtilities, $TSAny, $TSContext, $TSObject } from 'amplify-cli-core';
* @param {$TSAny[]} jobs - A list of jobs to run.
* @param {$TSAny} props - The props to use for variable replacement.
* @param {boolean} force - Force CF template generation
* @param {$TSAny[]|$TSObject} writeParams - data for the CF template but not parameters file
* @param {$TSAny[]|$TSObject} writeParams - boolean of whether props should be written to job.paramsFile, or an object that should be written to job.paramsFile
*/
export async function copyBatch(context: $TSContext, jobs: $TSAny, props: $TSAny, force: boolean, writeParams?: $TSAny[] | $TSObject) {
export async function copyBatch(context: $TSContext, jobs: $TSCopyJob, props: object, force?: boolean, writeParams?: boolean | object) {
// grab some features
const { template, prompt, filesystem } = context as $TSAny;
const { template, prompt, filesystem } = context;
const { confirm } = prompt;

// If the file exists
const shouldGenerate = async (target: $TSAny, force: boolean) => {
const shouldGenerate = async (target: string, force?: boolean) => {
if (!filesystem.exists(target) || force) return true;
return confirm(`overwrite ${target}`);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import * as path from 'path';
<<<<<<< HEAD
import { JSONUtilities, pathManager, $TSAny, $TSContext } from 'amplify-cli-core';
export function readBreadcrumbs(context: $TSContext, category: string, resourceName: string): $TSAny {
const breadcrumbsPath = path.join(pathManager.getBackendDirPath(), category, resourceName, context.amplify.constants.BreadcrumbsFileName);
=======
import { JSONUtilities, pathManager } from 'amplify-cli-core';
import { amplifyCLIConstants } from './constants';
import { leaveBreadcrumbs } from './leave-breadcrumbs';

export function readBreadcrumbs(category: string, resourceName: string) {
const breadcrumbsPath = path.join(pathManager.getBackendDirPath(), category, resourceName, amplifyCLIConstants.BreadcrumbsFileName);
>>>>>>> chore: separate func invoke and build
let breadcrumbs = JSONUtilities.readJson(breadcrumbsPath, {
throwIfNotExist: false,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { JSONUtilities, pathManager, stateManager, $TSAny, $TSMeta, $TSObject, R
import { ServiceName } from 'amplify-category-function';
import _ from 'lodash';
import { BuildType } from 'amplify-function-plugin-interface';
import { buildTypeKeyMap } from 'amplify-category-function';

export function updateAwsMetaFile(
filePath: string,
Expand Down Expand Up @@ -191,11 +192,6 @@ export function updateamplifyMetaAfterBuild({ category, resourceName }: Resource
stateManager.setMeta(undefined, amplifyMeta);
}

const buildTypeKeyMap: Record<BuildType, string> = {
[BuildType.PROD]: 'lastBuildTimeStamp',
[BuildType.DEV]: 'lastDevBuildTimeStamp',
};

export function updateAmplifyMetaAfterPackage({ category, resourceName }: ResourceTuple, zipFilename: string) {
const amplifyMeta = stateManager.getMeta();
_.set(amplifyMeta, [category, resourceName, 'lastPackageTimeStamp'], new Date());
Expand Down
7 changes: 3 additions & 4 deletions packages/amplify-function-plugin-interface/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { $TSContext } from 'amplify-cli-core';
/*
Function Runtime Contributor Types
*/

// All Function Runtime Contributor plugins must export a function of this type named 'functionRuntimeContributorFactory'
export type FunctionRuntimeContributorFactory = (
context: $TSContext,
context: any,
) => Contributor<FunctionRuntimeParameters, RuntimeContributionRequest> & FunctionRuntimeLifecycleManager;

// Subset of FunctionParameters that defines the function runtime
Expand Down Expand Up @@ -74,8 +73,8 @@ export type BuildRequest = {
};

export enum BuildType {
PROD,
DEV,
PROD = 'PROD',
DEV = 'DEV',
}

// Request sent to package a function
Expand Down
1 change: 1 addition & 0 deletions packages/amplify-util-mock/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@types/semver": "^7.1.0",
"@types/which": "^1.3.2",
"amplify-cli-core":"^1.14.1",
"amplify-function-plugin-interface": "1.6.0",
"aws-appsync": "^2.0.2",
"aws-sdk": "^2.765.0",
"aws-sdk-mock": "^5.1.0",
Expand Down
Loading

0 comments on commit c8a3c55

Please sign in to comment.