Skip to content

Commit 921c95e

Browse files
add prettier & prettier-ignore & pre-commit hook (#16)
1 parent 8a48029 commit 921c95e

File tree

6 files changed

+2480
-112
lines changed

6 files changed

+2480
-112
lines changed

Diff for: .prettierignore

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Markdown
2+
*.md
3+
4+
# Logs
5+
logs
6+
*.log
7+
npm-debug.log*
8+
9+
# Dependency directories
10+
node_modules
11+
12+
# Optional npm cache directory
13+
.npm
14+
15+
# Optional REPL history
16+
.node_repl_history
17+
.next

Diff for: .prettierrc.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"@netlify/eslint-config-node/.prettierrc.json"

Diff for: helpers/isStaticExportProject.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
// the Next.js app uses static HTML export
44

55
const isStaticExportProject = ({ build, scripts }) => {
6-
const NEXT_EXPORT_COMMAND = 'next export';
7-
const isSetInNetlifyConfig = build && build.command && build.command.includes(NEXT_EXPORT_COMMAND);
6+
const NEXT_EXPORT_COMMAND = 'next export'
7+
const isSetInNetlifyConfig = build && build.command && build.command.includes(NEXT_EXPORT_COMMAND)
88
const isSetInNpmScript = Object.keys(scripts).find((script) => {
9-
return scripts[script].includes(NEXT_EXPORT_COMMAND);
10-
});
11-
return isSetInNetlifyConfig || isSetInNpmScript;
12-
};
9+
return scripts[script].includes(NEXT_EXPORT_COMMAND)
10+
})
11+
return isSetInNetlifyConfig || isSetInNpmScript
12+
}
1313

14-
module.exports = isStaticExportProject;
14+
module.exports = isStaticExportProject

Diff for: index.js

+65-63
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
const fs = require('fs');
2-
const { existsSync, readFileSync } = require('fs');
3-
const path = require('path');
4-
const { appendFile, readdir } = require('fs').promises;
5-
const { hasFramework } = require('@netlify/framework-info');
6-
const nextOnNetlify = require('next-on-netlify');
7-
const { PHASE_PRODUCTION_BUILD } = require('next/constants');
8-
const { default: loadConfig } = require('next/dist/next-server/server/config');
9-
const makeDir = require('make-dir');
10-
const cpx = require('cpx');
11-
const isStaticExportProject = require('./helpers/isStaticExportProject');
1+
const fs = require('fs')
2+
const { existsSync, readFileSync } = require('fs')
3+
const path = require('path')
4+
const { appendFile, readdir } = require('fs').promises
5+
const { hasFramework } = require('@netlify/framework-info')
6+
const nextOnNetlify = require('next-on-netlify')
7+
const { PHASE_PRODUCTION_BUILD } = require('next/constants')
8+
const { default: loadConfig } = require('next/dist/next-server/server/config')
9+
const makeDir = require('make-dir')
10+
const cpx = require('cpx')
11+
const isStaticExportProject = require('./helpers/isStaticExportProject')
1212

1313
// * Helpful Plugin Context *
1414
// - Between the prebuild and build steps, the project's build command is run
@@ -17,71 +17,73 @@ const isStaticExportProject = require('./helpers/isStaticExportProject');
1717
module.exports = {
1818
async onPreBuild({ netlifyConfig, packageJson: { scripts = {}, dependencies = {} }, utils }) {
1919
if (!(await hasFramework('next'))) {
20-
return failBuild(`This application does not use Next.js.`);
20+
return failBuild(`This application does not use Next.js.`)
2121
}
2222

23-
const { build } = netlifyConfig;
24-
const { failBuild } = utils.build;
23+
const { build } = netlifyConfig
24+
const { failBuild } = utils.build
2525

26-
// TO-DO: Post alpha, try to remove this workaround for missing deps in
27-
// the next-on-netlify function template
28-
await utils.run.command('npm install next-on-netlify@latest');
26+
// TO-DO: Post alpha, try to remove this workaround for missing deps in
27+
// the next-on-netlify function template
28+
await utils.run.command('npm install next-on-netlify@latest')
2929

30-
if (isStaticExportProject({ build, scripts })) {
31-
failBuild(`** Static HTML export next.js projects do not require this plugin **`);
32-
}
30+
if (isStaticExportProject({ build, scripts })) {
31+
failBuild(`** Static HTML export next.js projects do not require this plugin **`)
32+
}
3333

34-
// TO-DO: check scripts to make sure the app isn't manually running NoN
35-
// For now, we'll make it clear in the README
36-
// const isAlreadyUsingNextOnNetlify = Object.keys(dependencies).find((dep) => dep === 'next-on-netlify');
37-
// if (isAlreadyUsingNextOnNetlify) {
38-
// failBuild(`This plugin cannot support apps that manually use next-on-netlify. Uninstall next-on-netlify as a dependency to resolve.`);
39-
// }
34+
// TO-DO: check scripts to make sure the app isn't manually running NoN
35+
// For now, we'll make it clear in the README
36+
// const isAlreadyUsingNextOnNetlify = Object.keys(dependencies).find((dep) => dep === 'next-on-netlify');
37+
// if (isAlreadyUsingNextOnNetlify) {
38+
// failBuild(`This plugin cannot support apps that manually use next-on-netlify. Uninstall next-on-netlify as a dependency to resolve.`);
39+
// }
4040

41-
const isFunctionsDirectoryCorrect = build && build.functions && build.functions === path.resolve('out_functions');
42-
if (!isFunctionsDirectoryCorrect) {
43-
// to do rephrase
44-
failBuild(`You must designate a functions directory named "out_functions" in your netlify.toml or in your app's build settings on Netlify. See docs for more info: https://docs.netlify.com/functions/configure-and-deploy/#configure-the-functions-folder`);
45-
}
41+
const isFunctionsDirectoryCorrect = build && build.functions && build.functions === path.resolve('out_functions')
42+
if (!isFunctionsDirectoryCorrect) {
43+
// to do rephrase
44+
failBuild(
45+
`You must designate a functions directory named "out_functions" in your netlify.toml or in your app's build settings on Netlify. See docs for more info: https://docs.netlify.com/functions/configure-and-deploy/#configure-the-functions-folder`,
46+
)
47+
}
4648

47-
if (existsSync('next.config.js')) {
48-
// If the next config exists, fail build if target isnt in acceptableTargets
49-
const acceptableTargets = ['serverless', 'experimental-serverless-trace'];
50-
const nextConfig = loadConfig(PHASE_PRODUCTION_BUILD, path.resolve('.'));
51-
const isValidTarget = acceptableTargets.includes(nextConfig.target);
52-
if (!isValidTarget) {
53-
failBuild(`next.config.js must be one of: ${acceptableTargets.join(', ')}`);
54-
}
55-
} else {
56-
// Create the next config file with target set to serverless by default
57-
const nextConfig = `
49+
if (existsSync('next.config.js')) {
50+
// If the next config exists, fail build if target isnt in acceptableTargets
51+
const acceptableTargets = ['serverless', 'experimental-serverless-trace']
52+
const nextConfig = loadConfig(PHASE_PRODUCTION_BUILD, path.resolve('.'))
53+
const isValidTarget = acceptableTargets.includes(nextConfig.target)
54+
if (!isValidTarget) {
55+
failBuild(`next.config.js must be one of: ${acceptableTargets.join(', ')}`)
56+
}
57+
} else {
58+
// Create the next config file with target set to serverless by default
59+
const nextConfig = `
5860
module.exports = {
5961
target: 'serverless'
6062
}
61-
`;
62-
await appendFile('next.config.js', nextConfig);
63-
console.log(`** Adding next.config.js with target set to 'serverless' **`);
64-
}
63+
`
64+
await appendFile('next.config.js', nextConfig)
65+
console.log(`** Adding next.config.js with target set to 'serverless' **`)
66+
}
6567
},
6668
async onBuild({ constants }) {
67-
console.log(`** Running Next on Netlify package **`);
68-
nextOnNetlify();
69+
console.log(`** Running Next on Netlify package **`)
70+
nextOnNetlify()
6971

70-
// Next-on-netlify puts its files into out_functions and out_publish
71-
// Copy files from next-on-netlify's output to the right functions/publish dirs
72+
// Next-on-netlify puts its files into out_functions and out_publish
73+
// Copy files from next-on-netlify's output to the right functions/publish dirs
7274

73-
// TO-DO: use FUNCTIONS_DIST when internal bug is fixed
74-
const { PUBLISH_DIR } = constants;
75-
// if (!existsSync(FUNCTIONS_DIST)) {
76-
// await makeDir(FUNCTIONS_DIST);
77-
// }
78-
if (!existsSync(PUBLISH_DIR)) {
79-
await makeDir(PUBLISH_DIR);
80-
}
75+
// TO-DO: use FUNCTIONS_DIST when internal bug is fixed
76+
const { PUBLISH_DIR } = constants
77+
// if (!existsSync(FUNCTIONS_DIST)) {
78+
// await makeDir(FUNCTIONS_DIST);
79+
// }
80+
if (!existsSync(PUBLISH_DIR)) {
81+
await makeDir(PUBLISH_DIR)
82+
}
8183

82-
// TO-DO: make sure FUNCTIONS_DIST doesnt have a custom function name conflict
83-
// with function names that next-on-netlify can generate
84-
// cpx.copySync('out_functions/**/*', FUNCTIONS_SRC);
85-
cpx.copySync('out_publish/**/*', PUBLISH_DIR);
86-
}
84+
// TO-DO: make sure FUNCTIONS_DIST doesnt have a custom function name conflict
85+
// with function names that next-on-netlify can generate
86+
// cpx.copySync('out_functions/**/*', FUNCTIONS_SRC);
87+
cpx.copySync('out_publish/**/*', PUBLISH_DIR)
88+
},
8789
}

0 commit comments

Comments
 (0)