Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 13 additions & 21 deletions src/init/features/functions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,8 @@
it("creates a new javascript codebase with the correct configuration", async () => {
const setup = { config: { functions: [] }, rcfile: {} };
prompt.select.onFirstCall().resolves("javascript");

// say "yes" to enabling eslint for the js project
prompt.confirm.onFirstCall().resolves(true);
// do not install dependencies
prompt.confirm.onSecondCall().resolves(false);
prompt.confirm.onFirstCall().resolves(false);
askWriteProjectFileStub = sandbox.stub(emptyConfig, "askWriteProjectFile");
askWriteProjectFileStub.resolves();

Expand All @@ -86,11 +83,11 @@
source: TEST_SOURCE_DEFAULT,
codebase: TEST_CODEBASE_DEFAULT,
ignore: ["node_modules", ".git", "firebase-debug.log", "firebase-debug.*.log", "*.local"],
predeploy: ['npm --prefix "$RESOURCE_DIR" run lint'],
predeploy: [],
});
expect(askWriteProjectFileStub.getCalls().map((call) => call.args[0])).to.deep.equal([

Check warning on line 88 in src/init/features/functions.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe return of an `any` typed value
`${TEST_SOURCE_DEFAULT}/package.json`,
`${TEST_SOURCE_DEFAULT}/.eslintrc.js`,
`${TEST_SOURCE_DEFAULT}/biome.json`,
`${TEST_SOURCE_DEFAULT}/index.js`,
`${TEST_SOURCE_DEFAULT}/.gitignore`,
]);
Expand All @@ -99,10 +96,8 @@
it("creates a new typescript codebase with the correct configuration", async () => {
const setup = { config: { functions: [] }, rcfile: {} };
prompt.select.onFirstCall().resolves("typescript");
// Lint
prompt.confirm.onFirstCall().resolves(true);
// do not install dependencies
prompt.confirm.onSecondCall().resolves(false);
prompt.confirm.onFirstCall().resolves(false);
askWriteProjectFileStub = sandbox.stub(emptyConfig, "askWriteProjectFile");
askWriteProjectFileStub.resolves();

Expand All @@ -117,10 +112,9 @@
'npm --prefix "$RESOURCE_DIR" run build',
],
});
expect(askWriteProjectFileStub.getCalls().map((call) => call.args[0])).to.deep.equal([

Check warning on line 115 in src/init/features/functions.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe return of an `any` typed value
`${TEST_SOURCE_DEFAULT}/package.json`,
`${TEST_SOURCE_DEFAULT}/.eslintrc.js`,
`${TEST_SOURCE_DEFAULT}/tsconfig.dev.json`,
`${TEST_SOURCE_DEFAULT}/biome.json`,
`${TEST_SOURCE_DEFAULT}/tsconfig.json`,
`${TEST_SOURCE_DEFAULT}/src/index.ts`,
`${TEST_SOURCE_DEFAULT}/.gitignore`,
Expand All @@ -138,9 +132,8 @@

// Initialize as JavaScript
prompt.select.onSecondCall().resolves("javascript");
// Lint but do not install dependencies
prompt.confirm.onFirstCall().resolves(true);
prompt.confirm.onSecondCall().resolves(false);
// do not install dependencies
prompt.confirm.onFirstCall().resolves(false);
askWriteProjectFileStub = sandbox.stub(config, "askWriteProjectFile");
askWriteProjectFileStub.resolves();

Expand Down Expand Up @@ -169,12 +162,12 @@
"firebase-debug.*.log",
"*.local",
],
predeploy: ['npm --prefix "$RESOURCE_DIR" run lint'],
predeploy: [],
},
]);
expect(askWriteProjectFileStub.getCalls().map((call) => call.args[0])).to.deep.equal([

Check warning on line 168 in src/init/features/functions.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe return of an `any` typed value
`testsource2/package.json`,
`testsource2/.eslintrc.js`,
`testsource2/biome.json`,
`testsource2/index.js`,
`testsource2/.gitignore`,
]);
Expand All @@ -185,9 +178,8 @@
prompt.select.onFirstCall().resolves("reinit");
prompt.select.onSecondCall().resolves("javascript");

// Lint but do not install dependencies
prompt.confirm.onFirstCall().resolves(true);
prompt.confirm.onSecondCall().resolves(false);
// do not install dependencies
prompt.confirm.onFirstCall().resolves(false);
askWriteProjectFileStub = sandbox.stub(config, "askWriteProjectFile");
askWriteProjectFileStub.resolves();

Expand All @@ -204,12 +196,12 @@
"firebase-debug.*.log",
"*.local",
],
predeploy: ['npm --prefix "$RESOURCE_DIR" run lint'],
predeploy: [],
},
]);
expect(askWriteProjectFileStub.getCalls().map((call) => call.args[0])).to.deep.equal([

Check warning on line 202 in src/init/features/functions.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe return of an `any` typed value
`${TEST_SOURCE_DEFAULT}/package.json`,
`${TEST_SOURCE_DEFAULT}/.eslintrc.js`,
`${TEST_SOURCE_DEFAULT}/biome.json`,
`${TEST_SOURCE_DEFAULT}/index.js`,
`${TEST_SOURCE_DEFAULT}/.gitignore`,
]);
Expand Down
39 changes: 10 additions & 29 deletions src/init/features/functions/javascript.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,23 @@
import { askInstallDependencies } from "./npm-dependencies";
import { confirm } from "../../../prompt";
import { configForCodebase } from "../../../functions/projectConfig";
import { readTemplateSync } from "../../../templates";
import * as supported from "../../../deploy/functions/runtimes/supported";

const INDEX_TEMPLATE = readTemplateSync("init/functions/javascript/index.js");
const PACKAGE_LINTING_TEMPLATE = readTemplateSync("init/functions/javascript/package.lint.json");
const PACKAGE_NO_LINTING_TEMPLATE = readTemplateSync(
"init/functions/javascript/package.nolint.json",
);
const ESLINT_TEMPLATE = readTemplateSync("init/functions/javascript/_eslintrc");
const PACKAGE_TEMPLATE = readTemplateSync("init/functions/javascript/package.json");
const BIOME_TEMPLATE = readTemplateSync("init/functions/javascript/biome.json");
const GITIGNORE_TEMPLATE = readTemplateSync("init/functions/javascript/_gitignore");

export async function setup(setup: any, config: any): Promise<any> {

Check warning on line 11 in src/init/features/functions/javascript.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type

Check warning on line 11 in src/init/features/functions/javascript.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type

Check warning on line 11 in src/init/features/functions/javascript.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type

Check warning on line 11 in src/init/features/functions/javascript.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
setup.functions.lint =
setup.functions.lint ||
(await confirm("Do you want to use ESLint to catch probable bugs and enforce style?"));
if (setup.functions.lint) {
const cbconfig = configForCodebase(setup.config.functions, setup.functions.codebase);
cbconfig.predeploy = ['npm --prefix "$RESOURCE_DIR" run lint'];
await config.askWriteProjectFile(
`${setup.functions.source}/package.json`,
PACKAGE_LINTING_TEMPLATE.replace(
"{{RUNTIME}}",
supported.latest("nodejs").replace("nodejs", ""),
),
);
await config.askWriteProjectFile(`${setup.functions.source}/.eslintrc.js`, ESLINT_TEMPLATE);
} else {
await config.askWriteProjectFile(
`${setup.functions.source}/package.json`,
PACKAGE_NO_LINTING_TEMPLATE.replace(
"{{RUNTIME}}",
supported.latest("nodejs").replace("nodejs", ""),
),
);
}
const cbconfig = configForCodebase(setup.config.functions, setup.functions.codebase);

Check warning on line 12 in src/init/features/functions/javascript.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .config on an `any` value

Check warning on line 12 in src/init/features/functions/javascript.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe argument of type `any` assigned to a parameter of type `ValidatedConfig`
cbconfig.predeploy = [];

const runtime = supported.latest("nodejs").replace("nodejs", "");
await config.askWriteProjectFile(
`${setup.functions.source}/package.json`,
PACKAGE_TEMPLATE.replace("{{RUNTIME}}", runtime),
);
await config.askWriteProjectFile(`${setup.functions.source}/biome.json`, BIOME_TEMPLATE);
await config.askWriteProjectFile(`${setup.functions.source}/index.js`, INDEX_TEMPLATE);
await config.askWriteProjectFile(`${setup.functions.source}/.gitignore`, GITIGNORE_TEMPLATE);
await askInstallDependencies(setup.functions, config);
Expand Down
54 changes: 12 additions & 42 deletions src/init/features/functions/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,28 @@
import { askInstallDependencies } from "./npm-dependencies";
import { confirm } from "../../../prompt";
import { configForCodebase } from "../../../functions/projectConfig";
import { readTemplateSync } from "../../../templates";
import * as supported from "../../../deploy/functions/runtimes/supported";

const PACKAGE_LINTING_TEMPLATE = readTemplateSync("init/functions/typescript/package.lint.json");
const PACKAGE_NO_LINTING_TEMPLATE = readTemplateSync(
"init/functions/typescript/package.nolint.json",
);
const ESLINT_TEMPLATE = readTemplateSync("init/functions/typescript/_eslintrc");
const PACKAGE_TEMPLATE = readTemplateSync("init/functions/typescript/package.json");
const BIOME_TEMPLATE = readTemplateSync("init/functions/typescript/biome.json");
const TSCONFIG_TEMPLATE = readTemplateSync("init/functions/typescript/tsconfig.json");
const TSCONFIG_DEV_TEMPLATE = readTemplateSync("init/functions/typescript/tsconfig.dev.json");
const INDEX_TEMPLATE = readTemplateSync("init/functions/typescript/index.ts");
const GITIGNORE_TEMPLATE = readTemplateSync("init/functions/typescript/_gitignore");

export async function setup(setup: any, config: any): Promise<any> {
setup.functions.lint =
setup.functions.lint ||
(await confirm({
message: "Do you want to use ESLint to catch probable bugs and enforce style?",
default: true,
}));

const cbconfig = configForCodebase(setup.config.functions, setup.functions.codebase);
cbconfig.predeploy = [];
if (setup.functions.lint) {
cbconfig.predeploy.push('npm --prefix "$RESOURCE_DIR" run lint');
cbconfig.predeploy.push('npm --prefix "$RESOURCE_DIR" run build');
await config.askWriteProjectFile(
`${setup.functions.source}/package.json`,
PACKAGE_LINTING_TEMPLATE.replace(
"{{RUNTIME}}",
supported.latest("nodejs").replace("nodejs", ""),
),
);
await config.askWriteProjectFile(`${setup.functions.source}/.eslintrc.js`, ESLINT_TEMPLATE);
// TODO: isn't this file out of date now?
await config.askWriteProjectFile(
`${setup.functions.source}/tsconfig.dev.json`,
TSCONFIG_DEV_TEMPLATE,
);
} else {
cbconfig.predeploy.push('npm --prefix "$RESOURCE_DIR" run build');
await config.askWriteProjectFile(
`${setup.functions.source}/package.json`,
PACKAGE_NO_LINTING_TEMPLATE.replace(
"{{RUNTIME}}",
supported.latest("nodejs").replace("nodejs", ""),
),
);
}
cbconfig.predeploy = [
'npm --prefix "$RESOURCE_DIR" run lint',
'npm --prefix "$RESOURCE_DIR" run build',
];

const runtime = supported.latest("nodejs").replace("nodejs", "");
await config.askWriteProjectFile(
`${setup.functions.source}/package.json`,
PACKAGE_TEMPLATE.replace("{{RUNTIME}}", runtime),
);
await config.askWriteProjectFile(`${setup.functions.source}/biome.json`, BIOME_TEMPLATE);
await config.askWriteProjectFile(`${setup.functions.source}/tsconfig.json`, TSCONFIG_TEMPLATE);

await config.askWriteProjectFile(`${setup.functions.source}/src/index.ts`, INDEX_TEMPLATE);
await config.askWriteProjectFile(`${setup.functions.source}/.gitignore`, GITIGNORE_TEMPLATE);
await askInstallDependencies(setup.functions, config);
Expand Down
28 changes: 0 additions & 28 deletions templates/init/functions/javascript/_eslintrc

This file was deleted.

13 changes: 13 additions & 0 deletions templates/init/functions/javascript/biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"$schema": "https://biomejs.dev/schemas/latest/schema.json",
"formatter": {
"enabled": true,
"indentStyle": "space"
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
}
}
46 changes: 21 additions & 25 deletions templates/init/functions/javascript/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
/**
* Import function triggers from their respective submodules:
*
* const {onCall} = require("firebase-functions/v2/https");
* const {onDocumentWritten} = require("firebase-functions/v2/firestore");
*
* See a full list of supported triggers at https://firebase.google.com/docs/functions
*/
import { setGlobalOptions } from "firebase-functions";
import * as logger from "firebase-functions/logger";
import { onRequest } from "firebase-functions/https";

const {setGlobalOptions} = require("firebase-functions");
const {onRequest} = require("firebase-functions/https");
const logger = require("firebase-functions/logger");
// Start writing functions:
// https://firebase.google.com/docs/functions

// For cost control, you can set the maximum number of containers that can be
// running at the same time. This helps mitigate the impact of unexpected
// traffic spikes by instead downgrading performance. This limit is a
// per-function limit. You can override the limit for each function using the
// `maxInstances` option in the function's options, e.g.
// `onRequest({ maxInstances: 5 }, (req, res) => { ... })`.
// NOTE: setGlobalOptions does not apply to functions using the v1 API. V1
// functions should each use functions.runWith({ maxInstances: 10 }) instead.
// In the v1 API, each function can only serve one request per container, so
// this will be the maximum concurrent request count.
setGlobalOptions({ maxInstances: 10 });
setGlobalOptions({
// For cost control, you can set the maximum number of containers that can be
// running at the same time. This helps mitigate the impact of unexpected
// traffic spikes by instead downgrading performance. This limit is a
// per-function limit. You can override the limit for each function using the
// `maxInstances` option in the function's options, e.g.
// `onRequest({ maxInstances: 5 }, (req, res) => { ... })`.
maxInstances: 10,
// Tip: event-driven triggers must run in the same region as the Firebase
// services they listen to (Firestore, Storage, etc.).
region: "us-central1",
});

// Create and deploy your first functions
// https://firebase.google.com/docs/functions/get-started

// exports.helloWorld = onRequest((request, response) => {
// logger.info("Hello logs!", {structuredData: true});
// response.send("Hello from Firebase!");
// });
export const helloWorld = onRequest((_request, response) => {
logger.info("Hello from Firebase!", { structuredData: true });
response.send("Hello from Firebase!");
});
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"type": "module",
"private": true,
"engines": {
"node": "{{RUNTIME}}"
},
"main": "index.js",
"scripts": {
"lint": "biome lint .",
"format": "biome format --write .",
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "{{RUNTIME}}"
},
"main": "index.js",
"dependencies": {
"firebase-admin": "^12.6.0",
"firebase-functions": "^6.0.1"
"firebase-admin": "^13.5.0",
"firebase-functions": "^6.4.0"
},
"devDependencies": {
"@biomejs/biome": "^2.2.5",
"firebase-functions-test": "^3.1.0"
},
"private": true
}
}
26 changes: 0 additions & 26 deletions templates/init/functions/javascript/package.lint.json

This file was deleted.

Loading
Loading