Skip to content
This repository was archived by the owner on May 10, 2021. It is now read-only.

Commit 63f5ce2

Browse files
committed
Add logger.js utility: Helpers for logging build info
The logger.js utility is a light wrapper on console.log. It takes care of bold formatting for titles and indents items by three spaces.
1 parent ebc0905 commit 63f5ce2

File tree

8 files changed

+46
-20
lines changed

8 files changed

+46
-20
lines changed

lib/copyNextAssets.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
const { join } = require('path')
22
const { copySync } = require('fs-extra')
3+
const { logTitle } = require('./logger')
34
const { NETLIFY_PUBLISH_PATH, NEXT_DIST_DIR } = require('./config')
45

56
// Copy the NextJS' static assets from NextJS distDir to Netlify publish folder.
67
// These need to be available for NextJS to work.
78
const copyNextAssets = () => {
8-
console.log(`\x1b[1m💼 Copying static NextJS assets to ${NETLIFY_PUBLISH_PATH}\x1b[22m`)
9+
logTitle("💼 Copying static NextJS assets to", NETLIFY_PUBLISH_PATH)
910
copySync(
1011
join(NEXT_DIST_DIR, "static"),
1112
join(NETLIFY_PUBLISH_PATH, "_next", "static"),

lib/copyPublicFiles.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { existsSync, copySync } = require('fs-extra')
2+
const { logTitle } = require('./logger')
23
const { NETLIFY_PUBLISH_PATH, PUBLIC_PATH } = require('./config')
34

45
// Copy files from public folder to Netlify publish folder
@@ -8,7 +9,7 @@ const copyPublicFiles = () => {
89
return
910

1011
// Perform copy operation
11-
console.log(`\x1b[1m🌍️ Copying ${PUBLIC_PATH} folder to ${NETLIFY_PUBLISH_PATH}\x1b[22m`)
12+
logTitle("🌍️ Copying", PUBLIC_PATH, "folder to", NETLIFY_PUBLISH_PATH)
1213
copySync(PUBLIC_PATH, NETLIFY_PUBLISH_PATH)
1314
}
1415

lib/logger.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Helpers for writing info to console.log
2+
3+
// Format in bold
4+
const logTitle = (...args) =>
5+
log(`\x1b[1m${args.join(" ")}\x1b[22m`)
6+
7+
// Indent by three spaces
8+
const logItem = (...args) =>
9+
log(' ', ...args)
10+
11+
// Just console.log
12+
const log = (...args) =>
13+
console.log(...args)
14+
15+
module.exports = {
16+
logTitle,
17+
logItem,
18+
log
19+
}

lib/prepareFolders.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
const { emptyDirSync } = require('fs-extra')
2+
const { logTitle, log } = require('./logger')
23
const { NETLIFY_PUBLISH_PATH, NETLIFY_FUNCTIONS_PATH } = require('./config')
34

45
// Empty existing publish and functions folders
56
const prepareFolders = () => {
6-
console.log("\x1b[1m🚀 Next on Netlify 🚀\x1b[22m")
7-
console.log(" Functions directory:", NETLIFY_PUBLISH_PATH)
8-
console.log(" Publish directory: ", NETLIFY_FUNCTIONS_PATH)
9-
console.log(" Make sure these are set in your netlify.toml file.")
7+
logTitle("🚀 Next on Netlify 🚀")
8+
log(" ", "Functions directory:", NETLIFY_PUBLISH_PATH)
9+
log(" ", "Publish directory: ", NETLIFY_FUNCTIONS_PATH)
10+
log(" ", "Make sure these are set in your netlify.toml file.")
1011

1112
emptyDirSync(NETLIFY_PUBLISH_PATH)
1213
emptyDirSync(NETLIFY_FUNCTIONS_PATH)

lib/setupHtmlPages.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
const path = require('path')
22
const { join } = path
33
const { copySync, existsSync } = require('fs-extra')
4+
const { logTitle, logItem } = require('./logger')
45
const { NEXT_DIST_DIR, NETLIFY_PUBLISH_PATH } = require('./config')
56
const allNextJsPages = require('./allNextJsPages')
67

78
// Identify all pages that have been pre-rendered and copy each one to the
89
// Netlify publish directory.
910
const setupHtmlPages = () => {
10-
console.log(`\x1b[1m📝 Writing pre-rendered HTML pages to ${NETLIFY_PUBLISH_PATH}\x1b[22m`)
11+
logTitle("📝 Writing pre-rendered HTML pages to", NETLIFY_PUBLISH_PATH)
1112

1213
// Get HTML pages
1314
const htmlPages = allNextJsPages.filter(page => page.isHtml())
1415

1516
// Copy each page to the Netlify publish directory
1617
htmlPages.forEach(({ filePath }) => {
17-
console.log(" ", filePath)
18+
logItem(filePath)
1819

1920
// The path to the file, relative to the pages directory
2021
const relativePath = path.relative("pages", filePath)

lib/setupRedirects.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { existsSync, readFileSync,
44
writeFileSync } = require('fs-extra')
55
const { default: isDynamicRoute } = require("@sls-next/lambda-at-edge/dist/lib/isDynamicRoute")
66
const { getSortedRoutes } = require("@sls-next/lambda-at-edge/dist/lib/sortedRoutes")
7+
const { logTitle, logItem } = require('./logger')
78
const { NETLIFY_PUBLISH_PATH,
89
CUSTOM_REDIRECTS_PATH } = require('./config')
910
const allNextJsPages = require('./allNextJsPages')
@@ -13,12 +14,12 @@ const getNetlifyFunctionName = require('./getNetlifyFunctionName')
1314
// Setup _redirects file that routes all requests to the appropriate location:
1415
// Either the relevant Netlify function or one of the pre-rendered HTML pages.
1516
const setupRedirects = () => {
16-
console.log("\x1b[1m🔀 Setting up redirects\x1b[22m")
17+
logTitle("🔀 Setting up redirects")
1718

1819
// Step 1: Collect custom redirects defined by the user
1920
const customRedirects = []
2021
if(existsSync(CUSTOM_REDIRECTS_PATH)) {
21-
console.log(" ", "# Prepending custom redirects")
22+
logItem("# Prepending custom redirects")
2223
customRedirects.push(readFileSync(CUSTOM_REDIRECTS_PATH))
2324
}
2425

@@ -75,7 +76,7 @@ const setupRedirects = () => {
7576

7677
// Assemble redirect
7778
const redirect = `${from} ${to} 200`
78-
console.log(" ", redirect)
79+
logItem(redirect)
7980

8081
nextOnNetlifyRedirects.push(redirect)
8182
})

lib/setupSsgPages.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const path = require('path')
22
const { join } = path
33
const { copySync, existsSync } = require('fs-extra')
4+
const { logTitle, logItem } = require('./logger')
45
const { NEXT_DIST_DIR, NETLIFY_PUBLISH_PATH,
56
NETLIFY_FUNCTIONS_PATH,
67
FUNCTION_TEMPLATE_PATH } = require('./config')
@@ -10,17 +11,17 @@ const getNetlifyFunctionName = require('./getNetlifyFunctionNam
1011
// Identify all pages that require server-side rendering and create a separate
1112
// Netlify Function for every page.
1213
const setupSsgPages = () => {
13-
console.log(`\x1b[1m🔥 Setting up SSG pages\x1b[22m`)
14+
logTitle("🔥 Setting up SSG pages")
1415

1516
// Get SSG pages
1617
const ssgPages = allNextJsPages.filter(page => page.isSsg())
1718

1819
// Copy pre-rendered SSG pages to Netlify publish folder
19-
console.log(" ", "1. Copying pre-rendered SSG pages to", NETLIFY_PUBLISH_PATH)
20+
logItem("1. Copying pre-rendered SSG pages to", NETLIFY_PUBLISH_PATH)
2021

2122
ssgPages.forEach(({ htmlFile }) => {
2223
const filePath = join("pages", htmlFile)
23-
console.log(" ", " ", filePath)
24+
logItem(" ", filePath)
2425

2526
copySync(
2627
join(NEXT_DIST_DIR, "serverless", filePath),
@@ -34,11 +35,11 @@ const setupSsgPages = () => {
3435

3536
// Copy SSG page data to _next/data/ folder
3637
const nextDataFolder = join(NETLIFY_PUBLISH_PATH, "_next", "data/")
37-
console.log(" ", "2. Copying SSG page data to", nextDataFolder)
38+
logItem("2. Copying SSG page data to", nextDataFolder)
3839

3940
ssgPages.forEach(({ jsonFile, dataRoute }) => {
4041
const dataPath = join("pages", jsonFile)
41-
console.log(" ", " ", dataPath)
42+
logItem(" ", dataPath)
4243

4344
copySync(
4445
join(NEXT_DIST_DIR, "serverless", dataPath),
@@ -52,10 +53,10 @@ const setupSsgPages = () => {
5253

5354
// Set up Netlify Functions to handle fallbacks for SSG pages
5455
const ssgFallbackPages = allNextJsPages.filter(page => page.isSsgFallback())
55-
console.log(" ", "3. Setting up Netlify Functions for SSG pages with fallback: true")
56+
logItem("3. Setting up Netlify Functions for SSG pages with fallback: true")
5657

5758
ssgFallbackPages.forEach(({ filePath }) => {
58-
console.log(" ", " ", filePath)
59+
logItem(" ", filePath)
5960

6061
// Set function name based on file path
6162
const functionName = getNetlifyFunctionName(filePath)

lib/setupSsrPages.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const path = require('path')
22
const { join } = path
33
const { copySync, existsSync } = require('fs-extra')
4+
const { logTitle, logItem } = require('./logger')
45
const { NEXT_DIST_DIR, NETLIFY_FUNCTIONS_PATH,
56
FUNCTION_TEMPLATE_PATH } = require('./config')
67
const allNextJsPages = require('./allNextJsPages')
@@ -9,14 +10,14 @@ const getNetlifyFunctionName = require('./getNetlifyF
910
// Identify all pages that require server-side rendering and create a separate
1011
// Netlify Function for every page.
1112
const setupSsrPages = () => {
12-
console.log(`\x1b[1m💫 Setting up SSR pages as Netlify Functions in ${NETLIFY_FUNCTIONS_PATH}\x1b[22m`)
13+
logTitle("💫 Setting up SSR pages as Netlify Functions in", NETLIFY_FUNCTIONS_PATH)
1314

1415
// Get SSR pages
1516
const ssrPages = allNextJsPages.filter(page => page.isSsr())
1617

1718
// Create Netlify Function for every page
1819
ssrPages.forEach(({ filePath }) => {
19-
console.log(" ", filePath)
20+
logItem(filePath)
2021

2122
// Set function name based on file path
2223
const functionName = getNetlifyFunctionName(filePath)

0 commit comments

Comments
 (0)