Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Warning for custom build scripts in project's package.json (#5240)
Warn if the build command in the source directory's package.json contains anything other than the default build command. Direct users towards the Express.js / custom integration
- Loading branch information
1 parent
97ce550
commit d7f0186
Showing
7 changed files
with
97 additions
and
0 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
- Add support for Firestore TTL (#5267) | ||
- Fix bug where secrets were not loaded when emulating functions with `--inpsect-functions`. (#4605) | ||
- Warn if a web framework's package.json contains anything other than the framework default build command. |
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,22 @@ | ||
import { readFile } from "fs/promises"; | ||
import { join } from "path"; | ||
|
||
/** | ||
* Prints a warning if the build script in package.json | ||
* contains anything other than allowedBuildScripts. | ||
*/ | ||
export async function warnIfCustomBuildScript( | ||
dir: string, | ||
framework: string, | ||
defaultBuildScripts: string[] | ||
): Promise<void> { | ||
const packageJsonBuffer = await readFile(join(dir, "package.json")); | ||
const packageJson = JSON.parse(packageJsonBuffer.toString()); | ||
const buildScript = packageJson.scripts?.build; | ||
|
||
if (buildScript && !defaultBuildScripts.includes(buildScript)) { | ||
console.warn( | ||
`\nWARNING: Your package.json contains a custom build that is being ignored. Only the ${framework} default build script (e.g, "${defaultBuildScripts[0]}") is respected. If you have a more advanced build process you should build a custom integration https://firebase.google.com/docs/hosting/express\n` | ||
); | ||
} | ||
} |
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,53 @@ | ||
import { expect } from "chai"; | ||
import * as sinon from "sinon"; | ||
import * as fs from "fs"; | ||
|
||
import { warnIfCustomBuildScript } from "../../frameworks/utils"; | ||
|
||
describe("Frameworks utils", () => { | ||
describe("warnIfCustomBuildScript", () => { | ||
const framework = "Next.js"; | ||
let sandbox: sinon.SinonSandbox; | ||
let consoleLogSpy: sinon.SinonSpy; | ||
const packageJson = { | ||
scripts: { | ||
build: "", | ||
}, | ||
}; | ||
|
||
beforeEach(() => { | ||
sandbox = sinon.createSandbox(); | ||
consoleLogSpy = sandbox.spy(console, "warn"); | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
it("should not print warning when a default build script is found.", async () => { | ||
const buildScript = "next build"; | ||
const defaultBuildScripts = ["next build"]; | ||
packageJson.scripts.build = buildScript; | ||
|
||
sandbox.stub(fs.promises, "readFile").resolves(JSON.stringify(packageJson)); | ||
|
||
await warnIfCustomBuildScript("fakedir/", framework, defaultBuildScripts); | ||
|
||
expect(consoleLogSpy.callCount).to.equal(0); | ||
}); | ||
|
||
it("should print warning when a custom build script is found.", async () => { | ||
const buildScript = "echo 'Custom build script' && next build"; | ||
const defaultBuildScripts = ["next build"]; | ||
packageJson.scripts.build = buildScript; | ||
|
||
sandbox.stub(fs.promises, "readFile").resolves(JSON.stringify(packageJson)); | ||
|
||
await warnIfCustomBuildScript("fakedir/", framework, defaultBuildScripts); | ||
|
||
expect(consoleLogSpy).to.be.calledOnceWith( | ||
`\nWARNING: Your package.json contains a custom build that is being ignored. Only the ${framework} default build script (e.g, "${defaultBuildScripts[0]}") is respected. If you have a more advanced build process you should build a custom integration https://firebase.google.com/docs/hosting/express\n` | ||
); | ||
}); | ||
}); | ||
}); |