Skip to content

Commit

Permalink
Refactored logger into utils so it can be tested. No tests written yet.
Browse files Browse the repository at this point in the history
  • Loading branch information
nataliecarey committed Mar 30, 2022
1 parent f0ba2bc commit 9e49433
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 77 deletions.
23 changes: 11 additions & 12 deletions __tests__/spec/sanity-checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
const assert = require('assert')
const fs = require('fs')
const path = require('path')
const util = require('util')

const glob = require('glob')
const request = require('supertest')
Expand Down Expand Up @@ -131,18 +130,18 @@ describe('The Prototype Kit', () => {
})
})

const sassFiles = glob.sync(gulpConfig.paths.assets + '/sass/*.scss')

describe(`${gulpConfig.paths.assets}sass/`, (done) => {
describe(`${gulpConfig.paths.assets}sass/`, () => {
const sassFiles = glob.sync(gulpConfig.paths.assets + '/sass/*.scss') && ['app/assets/sass/unbranded.scss']
it.each(sassFiles)('%s renders to CSS without errors', (file) => {
return sass.render({ file: file }, (err) => {
console.log('callback')
if (err) {
console.error('error found', err)
throw err
}
done()
})
const fullPath = path.resolve(__dirname, '..', '..', file)
const options = {
loadPaths: glob.sync(path.resolve(__dirname, '..', '..'))
}
console.log(fs.readFileSync(fullPath, 'utf8'))
console.log('compiling file', fullPath)
console.log('options', options)
const result = sass.compile(fullPath, options)
console.log(result.css)
})
})

Expand Down
74 changes: 10 additions & 64 deletions gulp/sass.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const path = require('path')
const fs = require('fs')

const extensions = require('../lib/extensions/extensions')
const customDartSassLoggerFactory = require('../lib/utils').customDartSassLoggerFactory
const config = require('./config.json')
const stylesheetDirectory = config.paths.public + 'stylesheets'

Expand All @@ -22,11 +23,11 @@ gulp.task('sass-extensions', function (done) {
})

gulp.task('sass', function () {
const deprecationWarningList = {}
const customLogger = customDartSassLoggerFactory()
return gulp.src(config.paths.assets + '/sass/*.scss', { sourcemaps: true })
.pipe(sass.sync({
outputStyle: 'expanded',
logger: customLogger(deprecationWarningList)
logger: customLogger.logger
}, false).on('error', function (error) {
// write a blank application.css to force browser refresh on error
if (!fs.existsSync(stylesheetDirectory)) {
Expand All @@ -37,89 +38,34 @@ gulp.task('sass', function () {
this.emit('end')
}))
.pipe(gulp.dest(stylesheetDirectory, { sourcemaps: true }))
.on('end', showDeprecationError(deprecationWarningList))
.on('end', customLogger.endHandler)
})

gulp.task('sass-documentation', function () {
const deprecationWarningList = {}
const customLogger = customDartSassLoggerFactory()
return gulp.src(config.paths.docsAssets + '/sass/*.scss', { sourcemaps: true })
.pipe(sass.sync({
outputStyle: 'expanded',
logger: customLogger(deprecationWarningList)
logger: customLogger.logger
}).on('error', sass.logError))
.pipe(gulp.dest(config.paths.public + '/stylesheets/', { sourcemaps: true }))
.on('end', showDeprecationError(deprecationWarningList))
.on('end', customLogger.endHandler)
})

// Backward compatibility with Elements

gulp.task('sass-v6', function () {
const deprecationWarningList = {}
const customLogger = customDartSassLoggerFactory()
return gulp.src(config.paths.v6Assets + '/sass/*.scss', { sourcemaps: true })
.pipe(sass.sync({
outputStyle: 'expanded',
logger: customLogger(deprecationWarningList),
logger: customLogger.logger,
includePaths: [
'node_modules/govuk_frontend_toolkit/stylesheets',
'node_modules/govuk-elements-sass/public/sass',
'node_modules/govuk_template_jinja/assets/stylesheets'
]
}).on('error', sass.logError))
.pipe(gulp.dest(config.paths.public + '/v6/stylesheets/', { sourcemaps: true }))
.on('end', showDeprecationError(deprecationWarningList))
.on('end', customLogger.endHandler)
})

// Custom logs to support the Dart Sass transition

function customLogger (warningList) {
function getFileNameFromMeta (meta) {
try {
return meta.span.file.url._uri.replace('file://' + path.resolve(__dirname, '..') + '/', '')
} catch (e) {
return ''
}
}

return {
debug: (debug, meta) => {
console.log(debug)
console.log(meta.stack)
},
warn: (warning, meta) => {
if (warning.match(/^[0-9]+ repetitive deprecation warnings omitted\.$/)) {
return
}
if (meta.deprecation === true && warning.includes('More info and automated migrator: https://sass-lang.com/d/slash-div')) {
const fileName = getFileNameFromMeta(meta)
const match = fileName.match(/node_modules[/\\]([^/\\]+)/)
const project = match ? match[1] : fileName
warningList[project] = warningList[project] || 0
warningList[project] += 1
} else {
const prefix = meta.deprecation ? 'DEPRECATION WARNING: ' : ''
console.error(`${prefix}${warning}`)
console.error(meta.stack)
}
}
}
}

function showDeprecationError (warningList) {
return function () {
if (Object.keys(warningList).length === 0) {
return;
}
const fmtStart = '\x1b[33m'
const fmtEnd = '\x1b[0m'
const em = '\x1b[4m'
const fmtRestart = `${fmtEnd}${fmtStart}`
Object.keys(warningList).forEach(project => console.warn(
`${fmtStart}${em}${project}${fmtRestart} uses a forward slash for division ${em}${warningList[project]} time${warningList[project] ? 's' : ''}${fmtRestart}.${fmtEnd}`))

console.error([
fmtStart,
'This is deprecated as documented at https://sass-lang.com/d/slash-div.',
'This won\'t cause any problems with your prototype but it would we would encourage them to move to Dart Sass.',
fmtEnd].join('\n'))
}
}
59 changes: 58 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ exports.findAvailablePort = function (app, callback) {
} else {
// User answers no - exit
console.log('\nYou can set a new default port in server.js, or by running the server with PORT=XXXX')
console.log("\nExit by pressing 'ctrl + c'")
console.log('\nExit by pressing \'ctrl + c\'')
process.exit(0)
}
})
Expand Down Expand Up @@ -278,3 +278,60 @@ exports.autoStoreData = function (req, res, next) {

next()
}

// Custom logs to support the Dart Sass transition

exports.customDartSassLoggerFactory = () => {
const warningList = {}
const getFileNameFromMeta = (meta) => {
try {
return meta.span.file.url._uri.replace('file://' + path.resolve(__dirname, '..') + '/', '')
} catch (e) {
return ''
}
}

return {
logger: {
debug: (debug, meta) => {
console.log(debug)
console.log(meta.stack)
},
warn: (warning, meta) => {
if (warning.match(/^[0-9]+ repetitive deprecation warnings omitted\.$/)) {
return
}
if (meta.deprecation === true && warning.includes('More info and automated migrator: https://sass-lang.com/d/slash-div')) {
const fileName = getFileNameFromMeta(meta)
const match = fileName.match(/node_modules[/\\]([^/\\]+)/)
const project = match ? match[1] : fileName
warningList[project] = warningList[project] || 0
warningList[project] += 1
} else {
const prefix = meta.deprecation ? 'DEPRECATION WARNING: ' : ''
console.error(`${prefix}${warning}`)
console.error(meta.stack)
}
}
},
endHandler: () => {
if (Object.keys(warningList).length === 0) {
return
}
const fmtStart = '\x1b[33m'
const fmtEnd = '\x1b[0m'
const em = '\x1b[4m'
const fmtRestart = `${fmtEnd}${fmtStart}`
Object.keys(warningList).forEach(project => console.warn(
`${fmtStart}${em}${project}${fmtRestart} uses a forward slash for division ${em}${warningList[project]} time${warningList[project] ? 's' : ''}${fmtRestart}.${fmtEnd}`))

if (Object.keys(warningList).length > 0) {
console.error([
fmtStart,
'This is deprecated as documented at https://sass-lang.com/d/slash-div.',
'This won\'t cause any problems with your prototype but it would we would encourage them to move to Dart Sass.',
fmtEnd].join('\n'))
}
}
}
}

0 comments on commit 9e49433

Please sign in to comment.