Skip to content

Commit

Permalink
Upgraded packages and removed support for V3 functions
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexPshul committed Nov 29, 2023
1 parent 42bcae1 commit 0e420b1
Show file tree
Hide file tree
Showing 22 changed files with 231 additions and 514 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"eslint.validate": ["json"],
"editor.codeActionsOnSave": {
"source.organizeImports": true
"source.organizeImports": "explicit"
}
}
102 changes: 4 additions & 98 deletions e2e/func-e2e/tests/func.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,100 +71,6 @@ describe('Project initialization and build', () => {

const funcFilePath = `apps/${project}/${func}/index.ts`;

updateFile(
funcFilePath,
` import { AzureFunction, Context, HttpRequest } from "@azure/functions"
import { ${lib} } from "@proj/${lib}";
const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
context.res = {
// status: 200, /* Defaults to 200 */
body: ${lib}()
};
};
export default httpTrigger;
`,
);

const buildResult = await runNxCommandAsync(`build ${project}`);

expect(buildResult.stdout).toContain(`Done compiling TypeScript files for project "${project}"`);
},
TEST_TIMEOUT,
);

it(
'Use strict mode',
async () => {
const project = uniq('func');
await runNxCommandAsync(`generate @nxazure/func:init ${project}`);

const tsConfig = await readJson<{ compilerOptions: CompilerOptions }>(`apps/${project}/tsconfig.json`);
const tsBuildConfig = await readJson<{ compilerOptions: CompilerOptions }>(`apps/${project}/tsconfig.build.json`);

expect(tsConfig.compilerOptions.strict).toBe(true);
expect(tsBuildConfig.compilerOptions.strict).toBe(true);
},
TEST_TIMEOUT,
);

it(
'Use no strict mode',
async () => {
const project = uniq('func');
await runNxCommandAsync(`generate @nxazure/func:init ${project} --no-strict`);

const tsConfig = await readJson<{ compilerOptions: CompilerOptions }>(`apps/${project}/tsconfig.json`);
const tsBuildConfig = await readJson<{ compilerOptions: CompilerOptions }>(`apps/${project}/tsconfig.build.json`);

expect(tsConfig.compilerOptions.strict).toBe(false);
expect(tsBuildConfig.compilerOptions.strict).toBe(false);
},
TEST_TIMEOUT,
);

it(
'should init & build and empty workspace with a functions app (V4)',
async () => {
const project = uniq('func');
await runNxCommandAsync(`generate @nxazure/func:init ${project} --v4`);
const buildResult = await runNxCommandAsync(`build ${project}`);

expect(buildResult.stdout).toContain(`Done compiling TypeScript files for project "${project}"`);
},
TEST_TIMEOUT,
);

it(
'should init & build a workspace with a functions app and a function (V4)',
async () => {
const project = uniq('func');
const func = 'hello';

await runNxCommandAsync(`generate @nxazure/func:init ${project} --v4`);
await runNxCommandAsync(`generate @nxazure/func:new ${func} --project=${project} --template="HTTP trigger"`);
const buildResult = await runNxCommandAsync(`build ${project}`);

expect(buildResult.stdout).toContain(`Done compiling TypeScript files for project "${project}"`);
},
TEST_TIMEOUT,
);

it(
'should init & build a workspace with a js lib functions app and a function (V4)',
async () => {
const project = uniq('func');
const lib = uniq('lib');
const func = 'hello';

await runNxCommandAsync(`generate @nxazure/func:init ${project} --v4`);
await runNxCommandAsync(`generate @nxazure/func:new ${func} --project=${project} --template="HTTP trigger"`);
await runNxCommandAsync(`generate @nx/js:library ${lib}`);

const funcFilePath = `apps/${project}/${func}/index.ts`;

updateFile(
funcFilePath,
` import { app, HttpRequest, HttpResponseInit, InvocationContext } from "@azure/functions";
Expand Down Expand Up @@ -192,10 +98,10 @@ describe('Project initialization and build', () => {
);

it(
'Use strict mode (V4)',
'Use strict mode',
async () => {
const project = uniq('func');
await runNxCommandAsync(`generate @nxazure/func:init ${project} --v4`);
await runNxCommandAsync(`generate @nxazure/func:init ${project}`);

const tsConfig = await readJson<{ compilerOptions: CompilerOptions }>(`apps/${project}/tsconfig.json`);
const tsBuildConfig = await readJson<{ compilerOptions: CompilerOptions }>(`apps/${project}/tsconfig.build.json`);
Expand All @@ -207,10 +113,10 @@ describe('Project initialization and build', () => {
);

it(
'Use no strict mode (V4)',
'Use no strict mode',
async () => {
const project = uniq('func');
await runNxCommandAsync(`generate @nxazure/func:init ${project} --no-strict --v4`);
await runNxCommandAsync(`generate @nxazure/func:init ${project} --no-strict`);

const tsConfig = await readJson<{ compilerOptions: CompilerOptions }>(`apps/${project}/tsconfig.json`);
const tsBuildConfig = await readJson<{ compilerOptions: CompilerOptions }>(`apps/${project}/tsconfig.build.json`);
Expand Down
72 changes: 72 additions & 0 deletions migrations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"migrations": [
{
"cli": "nx",
"version": "17.0.0-beta.1",
"description": "Updates the default cache directory to .nx/cache",
"implementation": "./src/migrations/update-17-0-0/move-cache-directory",
"package": "nx",
"name": "17.0.0-move-cache-directory"
},
{
"cli": "nx",
"version": "17.0.0-beta.3",
"description": "Use minimal config for tasksRunnerOptions",
"implementation": "./src/migrations/update-17-0-0/use-minimal-config-for-tasks-runner-options",
"package": "nx",
"name": "17.0.0-use-minimal-config-for-tasks-runner-options"
},
{
"version": "17.0.0-rc.1",
"description": "Migration for v17.0.0-rc.1",
"implementation": "./src/migrations/update-17-0-0/rm-default-collection-npm-scope",
"package": "nx",
"name": "rm-default-collection-npm-scope"
},
{
"cli": "nx",
"version": "16.9.0-beta.1",
"description": "Replace imports of Module Federation utils frm @nx/devkit to @nx/webpack",
"implementation": "./src/migrations/update-16-9-0/migrate-mf-util-usage",
"package": "@nx/devkit",
"name": "update-16-9-0-migrate-mf-usage-to-webpack"
},
{
"version": "17.0.0-beta.7",
"description": "update-17-0-0-rename-to-eslint",
"implementation": "./src/migrations/update-17-0-0-rename-to-eslint/update-17-0-0-rename-to-eslint",
"package": "@nx/linter",
"name": "update-17-0-0-rename-to-eslint"
},
{
"version": "17.1.0-beta.1",
"description": "Updates for @typescript-utils/utils v6.9.1+",
"implementation": "./src/migrations/update-17-1-0/update-typescript-eslint",
"package": "@nx/linter",
"name": "update-typescript-eslint"
},
{
"cli": "nx",
"version": "16.8.2-beta.0",
"description": "Remove invalid options (strict, noInterop) for ES6 type modules.",
"factory": "./src/migrations/update-16-8-2/update-swcrc",
"package": "@nx/js",
"name": "16-8-2-update-swcrc"
},
{
"cli": "nx",
"version": "17.0.2",
"description": "Remove deprecated build options",
"implementation": "./src/migrations/update-17-0-0/remove-deprecated-build-options",
"package": "@nx/js",
"name": "update-17-0-0-remove-deprecated-build-options"
},
{
"version": "17.1.0-beta.2",
"description": "Move jest executor options to nx.json targetDefaults",
"implementation": "./src/migrations/update-17-1-0/move-options-to-target-defaults",
"package": "@nx/jest",
"name": "move-options-to-target-defaults"
}
]
}
31 changes: 16 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,40 @@
"lint-staged": "lint-staged"
},
"devDependencies": {
"@nx/devkit": "16.8.1",
"@nx/eslint-plugin": "16.8.1",
"@nx/jest": "16.8.1",
"@nx/js": "16.8.1",
"@nx/linter": "16.8.1",
"@nx/plugin": "16.8.1",
"@nx/workspace": "16.8.1",
"@swc-node/register": "^1.4.2",
"@nx/devkit": "17.1.2",
"@nx/eslint-plugin": "17.1.2",
"@nx/jest": "17.1.2",
"@nx/js": "17.1.2",
"@nx/linter": "17.1.2",
"@nx/plugin": "17.1.2",
"@nx/workspace": "17.1.2",
"@swc-node/register": "1.6.8",
"@swc/cli": "0.1.62",
"@swc/core": "^1.2.173",
"@swc/core": "1.3.96",
"@types/jest": "29.4.0",
"@types/node": "^18.11.19",
"@typescript-eslint/eslint-plugin": "5.60.1",
"@typescript-eslint/parser": "5.60.1",
"@typescript-eslint/eslint-plugin": "6.9.1",
"@typescript-eslint/parser": "6.9.1",
"eslint": "8.46.0",
"eslint-config-prettier": "8.1.0",
"eslint-config-prettier": "9.0.0",
"husky": "^8.0.3",
"is-ci": "^3.0.1",
"jest": "29.4.3",
"jest-environment-jsdom": "29.4.3",
"jsonc-eslint-parser": "^2.1.0",
"lint-staged": "^13.1.0",
"nx": "16.8.1",
"nx": "17.1.2",
"prettier": "^2.6.2",
"ts-jest": "29.1.0",
"ts-node": "10.9.1",
"tsc-files": "^1.1.3",
"typescript": "5.1.6"
"typescript": "5.2.2"
},
"dependencies": {
"@swc/helpers": "0.5.1",
"@swc/helpers": "0.5.3",
"glob": "^10.2.2",
"tree-kill": "^1.2.2",
"tslib": "^2.3.0"
}
}

14 changes: 0 additions & 14 deletions packages/func/src/common/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Tree, readJson, readJsonFile } from '@nx/devkit';

export const color = {
error: (message: string) => `\x1B[31m${message}\x1B[0m`, // Red
warn: (message: string) => `\x1B[33m${message}\x1B[0m`, // Yellow
Expand All @@ -9,15 +7,3 @@ export const color = {
endpoint: (message: string) => `\x1B[38;2;19;161;14m${message}\x1B[0m`, // Green
fade: (message: string) => `\x1B[90m${message}\x1B[0m`, // Gray
};

type PackageJsonDependencies = { dependencies?: Record<string, string> };
const isV4PackageRegex = /^[^0-9]*4/;
export const isV4 = (tree?: Tree) => {
const { dependencies } = tree
? readJson<PackageJsonDependencies>(tree, 'package.json')
: readJsonFile<PackageJsonDependencies>('package.json');

const funcPackageVersion = dependencies?.['@azure/functions'];

return funcPackageVersion && isV4PackageRegex.test(funcPackageVersion);
};
23 changes: 5 additions & 18 deletions packages/func/src/executors/common/inject-path-registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,15 @@ import { readJsonFile } from '@nx/devkit';
import fs from 'fs';
import { glob } from 'glob';
import path from 'path';
import { isV4, registrationFileName } from '../../common';
import { registrationFileName } from '../../common';

const getFilesForPathInjection = async (appRoot: string) => {
if (isV4()) {
const { main: functionsPathPattern } = readJsonFile<{ main: string }>(path.join(appRoot, 'package.json'));
const { main: functionsPathPattern } = readJsonFile<{ main: string }>(path.join(appRoot, 'package.json'));

const functionsPath = path.posix.join(appRoot, functionsPathPattern);
const functions = await glob(functionsPath);
const functionsPath = path.posix.join(appRoot, functionsPathPattern);
const functions = await glob(functionsPath);

return functions;
} else {
const functionJsonFiles = await glob('**/function.json', { cwd: appRoot, ignore: ['**/node_modules/**'] });

return await Promise.all(
functionJsonFiles
.map(file => path.join(appRoot, file))
.map(async file => {
const { scriptFile } = await readJsonFile<{ scriptFile: string }>(file);
return path.join(path.dirname(file), scriptFile);
}),
);
}
return functions;
};

export const injectPathRegistration = async (outputPath: string, appRoot: string) => {
Expand Down
6 changes: 0 additions & 6 deletions packages/func/src/executors/publish/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Executor } from '@nx/devkit';
import { execSync } from 'child_process';
import fs from 'fs';
import path from 'path';
import { color, isV4 } from '../../common';
import { build } from '../common';
import { PublishExecutorSchema } from './schema';

Expand All @@ -12,11 +11,6 @@ const executor: Executor<PublishExecutorSchema> = async (options, context) => {
if (success) {
const { projectName, workspace, isVerbose, target } = context;

if (isV4()) {
console.log(color.warn('[V4 FUNCTION][ATTENTION]'));
console.log(color.warn('Add the configuration "AzureWebJobsFeatureFlags": "EnableWorkerIndexing" to your deployment.'));
}

const { name, additionalFlags } = options;
const installCommand = 'npm i';
if (isVerbose) {
Expand Down
12 changes: 6 additions & 6 deletions packages/func/src/generators/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import path from 'path';

const getEnvTempDir = () => process.env.RUNNER_TEMP || os.tmpdir(); // This supports not only local dev but also GitHub Actions

export const createTempFolderWithInit = (tempAppName: string, v4: boolean) => {
const tempName = names(tempAppName).fileName.split('/')
const tempFolder = fs.mkdtempSync(path.posix.join(getEnvTempDir(),`func-${tempName.pop()}-`));
export const createTempFolderWithInit = (tempAppName: string) => {
const tempName = names(tempAppName).fileName.split('/');
const tempFolder = fs.mkdtempSync(path.posix.join(getEnvTempDir(), `func-${tempName.pop()}-`));

try {
execSync(`func init ${tempAppName} --worker-runtime node --language typescript ${v4 ? '--model V4' : ''}`, {
execSync(`func init ${tempAppName} --worker-runtime node --language typescript`, {
cwd: tempFolder,
stdio: 'ignore',
});
Expand All @@ -23,7 +23,7 @@ export const createTempFolderWithInit = (tempAppName: string, v4: boolean) => {
return { tempFolder, tempProjectRoot: path.posix.join(tempFolder, tempAppName) };
};

export const copyToTempFolder = (tree: Tree, projectRootPath: string, v4: boolean) => {
export const copyToTempFolder = (tree: Tree, projectRootPath: string) => {
const tempFolder = fs.mkdtempSync(path.posix.join(getEnvTempDir(), `func-copy-`));

tree
Expand All @@ -33,7 +33,7 @@ export const copyToTempFolder = (tree: Tree, projectRootPath: string, v4: boolea
.map(({ filename, fullPath }) => ({ filename, content: tree.read(fullPath).toString() }))
.forEach(({ filename, content }) => fs.writeFileSync(path.posix.join(tempFolder, filename), content));

if (v4) fs.mkdirSync(path.posix.join(tempFolder, 'src/functions'), { recursive: true });
fs.mkdirSync(path.posix.join(tempFolder, 'src/functions'), { recursive: true });

return tempFolder;
};
Loading

0 comments on commit 0e420b1

Please sign in to comment.