Skip to content

Commit 27c00a8

Browse files
committed
rename to brandable_css
1 parent 98eb044 commit 27c00a8

14 files changed

Lines changed: 594 additions & 547 deletions

bin/brandable_css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env node
2+
3+
// without this, node only uses 4 of the available cores on my MacBookPro
4+
// see: http://www.future-processing.pl/blog/on-problems-with-threads-in-node-js/
5+
process.env.UV_THREADPOOL_SIZE = 6
6+
7+
require("babel/polyfill")
8+
require('../lib/cli')

bin/canvas_css

Lines changed: 0 additions & 2 deletions
This file was deleted.

npm-shrinkwrap.json

Lines changed: 290 additions & 290 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
2-
"name": "canvas_css",
3-
"version": "0.0.5",
4-
"description": "This is what we use to compile sass in canvas with all our variants and brand_css",
5-
"main": "index.js",
2+
"name": "brandable_css",
3+
"version": "0.0.1",
4+
"description": "This is what we use to compile sass in canvas-lms with all our variants and custom theme editor css",
65
"scripts": {
76
"build": "./node_modules/.bin/babel src --stage 0 --out-dir lib",
87
"dev": "npm run build -- --watch",
@@ -12,13 +11,13 @@
1211
},
1312
"repository": {
1413
"type": "git",
15-
"url": "https://github.com/ryankshaw/canvas_css.git"
14+
"url": "https://github.com/ryankshaw/brandable_css.git"
1615
},
1716
"author": "Ryan Shaw <ryankshaw@gmail.com>",
1817
"license": "MIT",
19-
"homepage": "https://github.com/ryankshaw/canvas_css",
18+
"homepage": "https://github.com/ryankshaw/brandable_css",
2019
"bin": {
21-
"canvas_css": "./bin/canvas_css"
20+
"canvas_css": "./bin/brandable_css"
2221
},
2322
"dependencies": {
2423
"autoprefixer": "^5.1.1",
@@ -31,7 +30,6 @@
3130
"glob": "^5.0.6",
3231
"js-yaml": "^3.3.1",
3332
"lodash": "^3.8.0",
34-
"mkdirp": "^0.5.1",
3533
"node-sass": "^3.1.0",
3634
"postcss": "^4.1.10",
3735
"postcss-url": "^3.2.0"

src/cache.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,40 @@
11
const debug = require('debug')('canvas_css')
22
import _ from 'lodash'
3-
import {readJsonSync, outputJson} from './utils'
4-
import PATHS from './paths'
3+
import {readJsonSync} from './utils'
4+
import {paths as PATHS} from "./config"
5+
const outputJson = Promise.promisify(require('fs-extra').outputJson)
56

67
const caches = ['file_checksums', 'bundles_with_deps']
78

89
let cache = {
910
saveAll(){
10-
return caches.map(cacheName => this[cacheName].save())
11+
return caches.map(cacheName => cache[cacheName].save())
1112
}
1213
}
1314

1415
function initCache(name) {
15-
const filename = PATHS['sass_' + name]
16+
const filename = PATHS[name]
1617
let self = {
1718
data: readJsonSync(filename),
1819
isSaved: false,
20+
1921
update(key, value) {
20-
if (self.data[key] == value) return
22+
if (_.isFunction(key)) throw new Error('cant use function as key' + key + value)
23+
if (self.data[key] == value) return value
2124
debug('updating cache key', key, value)
2225
self.isSaved = false
23-
self.data[key] = value
26+
return self.data[key] = value
2427
},
25-
save(data) {
28+
29+
save() {
2630
debug('saving', self.isSaved, filename)
2731
if (self.isSaved) return
2832
self.isSaved = true
2933
return outputJson(filename, self.data)
34+
},
35+
36+
clearMatching(query) {
37+
self.data = _.omit(self.data, (v, key) => key.match(query))
3038
}
3139
}
3240
cache[name] = self

src/checksum.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
import crypto from 'crypto'
22
import Promise from 'bluebird'
33
import fs from 'fs'
4+
import path from 'path'
45
import versions from './versions'
6+
import {paths as PATHS} from './config'
7+
58

69
function newHash() {
710
let hash = crypto.createHash('md5')
811
versions.forEach(::hash.update)
912
return hash
1013
}
1114

12-
export default function checksum (data) {
15+
export function checksum (data) {
1316
return newHash().update(data).digest('hex')
1417
}
1518

16-
export async function ofFile(filename) {
19+
export function fileChecksum (filename) {
1720
return new Promise(function(resolve, reject){
1821
var hsh = newHash()
1922
var s = fs.ReadStream(filename)
@@ -23,7 +26,11 @@ export async function ofFile(filename) {
2326
})
2427
}
2528

26-
export function ofFileSync(filename) {
29+
export function relativeFileChecksum(relativePath) {
30+
return fileChecksum(path.join(PATHS.sass_dir, relativePath))
31+
}
32+
33+
export function fileChecksumSync (filename) {
2734
try {
2835
return checksum(fs.readFileSync(filename))
2936
} catch(e) {}

src/cli.js

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,15 @@
11
import program from 'commander'
2-
import main from './main'
3-
import compileSingleBundle from './compile-bundle'
2+
import {checkAll, startWatcher} from './main'
3+
import {onError} from './utils'
44

55
program
66
.version(require('../package').version)
77
.option('--watch', 'watch for changes')
8-
.option('--bundle <bundle>', 'compile a specific bundle')
9-
.option('--variant <variant>', 'the variant (legacy_high_contrast, new_styles_normal_contrast, etc) you want')
10-
.option('--brand-variables-folder <brandVarablesFolder>', 'filesystem path to the folder that contains the _brand_variables.scss you want to use')
8+
// .option('--brand-config-md5 <md5>', 'compile just the styles for a specific Brand Config')
119

1210
program.parse(process.argv)
1311

14-
// if they specified the specific bundle they want, (eg: from the brand_css rails controller),
15-
// compile that one bundle and send it to stdout
16-
if (program.bundle) {
17-
compileSingleBundle({
18-
bundleName: program.bundle,
19-
variant: program.variant,
20-
brandVariablesFolder: program.brandVariablesFolder
21-
})
22-
.then(function(res){ console.log(res.css) })
23-
.catch(main.onError)
24-
} else {
25-
main.checkAll().catch(main.onError)
26-
if (program.watch) {
27-
main.watch()
28-
}
29-
}
30-
31-
32-
33-
12+
checkAll().catch(onError)
13+
if (program.watch) {
14+
startWatcher()
15+
}

src/compile-bundle.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,46 @@
1+
const debug = require('debug')('canvas_css:compile-bundle')
12
import Promise from 'bluebird'
23
const existsAsync = Promise.promisify(require('fs').stat)
34
const sassRender = Promise.promisify(require('node-sass').render)
4-
const debug = require('debug')('canvas_css:compile-bundle')
55
import path from 'path'
66
import _ from 'lodash'
77
import chalk from 'chalk'
88
import url from 'url'
99
import postcss from 'postcss'
1010
import autoprefixer from 'autoprefixer'
1111
import postcssUrl from 'postcss-url'
12-
import PATHS from './paths'
13-
import {ofFileSync} from './checksum'
12+
import {paths as PATHS} from "./config"
13+
import {BRANDABLE_VARIANTS} from './variants'
14+
import {fileChecksumSync} from './checksum'
1415
import supportedBrowsers from './browser-support'
1516
import cache from './cache'
16-
import {relativeSassPath} from './utils'
17+
import {relativeSassPath, folderForBrandId} from './utils'
18+
19+
// If an image is in css source as url("/images/foo/bar.png"),
20+
// Rails-asset-pipeline makes it available at the url: "/assets/foo/bar-{md5}.png"
21+
function removeFirstDir(dir) {
22+
return dir.split('/').slice(2).join('/')
23+
}
24+
function sprocketsFormattedUrl(originalUrl, md5) {
25+
let parsedUrl = url.parse(originalUrl)
26+
const {dir, name, ext} = path.posix.parse(parsedUrl.pathname)
27+
parsedUrl.pathname = `/assets/${removeFirstDir(dir)}/${name}-${md5}${ext}`
28+
return url.format(parsedUrl)
29+
}
1730

18-
// Any logging from compileSingleBundle needs to go to stderr so we can use stdout to
19-
// send css to rails controller when we compile a single bundle
2031
function warn() {
2132
console.error(chalk.yellow('canvas_css warning', ...arguments))
2233
}
2334

24-
export default async function compileSingleBundle ({bundleName, variant, brandVariablesFolder}) {
35+
export default async function compileSingleBundle ({bundleName, variant, brandId}) {
2536
const sassFile = path.join(PATHS.sass_dir, bundleName)
2637
let includePaths = [PATHS.sass_dir, path.join(PATHS.sass_dir, 'variants', variant)]
2738
// pull in 'config/brand_variables.scss' if we should
28-
if (brandVariablesFolder) {
29-
if (
30-
(variant === 'new_styles_normal_contrast' || variant === 'k12_normal_contrast') &&
31-
await existsAsync(path.join(brandVariablesFolder, '_brand_variables.scss'))
32-
) {
33-
includePaths.unshift(brandVariablesFolder)
34-
} else {
35-
throw new Error('invalid brandVariablesFolder or you tried to include it in a legacy or high contrast bundle')
36-
}
39+
if (brandId) {
40+
if (!BRANDABLE_VARIANTS.has(variant)) throw new Error(`${variant} is not brandable`)
41+
const fileExists = await existsAsync(path.join(folderForBrandId(brandId), '_brand_variables.scss'))
42+
if (!fileExists) throw new Error(`_brand_variables.scss file not found for ${brandId}`)
43+
includePaths.unshift(folderForBrandId(brandId))
3744
}
3845

3946
let urlsFoundInCss = new Set()
@@ -47,18 +54,15 @@ export default async function compileSingleBundle ({bundleName, variant, brandVa
4754
const relativePath = relativeSassPath(pathToFile)
4855
let md5 = cache.file_checksums.data[relativePath]
4956
if (!md5) {
50-
md5 = ofFileSync(pathToFile)
57+
md5 = fileChecksumSync(pathToFile)
5158
if (!md5) {
5259
warn(sassFile, variant, 'contains a url() to:', originalUrl, 'which doesn\'t exist on disk')
5360
return originalUrl
5461
}
5562
cache.file_checksums.update(relativePath, md5)
5663
}
5764
urlsFoundInCss.add(pathToFile)
58-
59-
const {dir, name, ext} = path.posix.parse(parsedUrl.pathname)
60-
parsedUrl.pathname = `/assets${dir}/${name}-${md5}${ext}`
61-
return url.format(parsedUrl)
65+
return sprocketsFormattedUrl(originalUrl, md5)
6266
}
6367

6468
const startTime = new Date()

src/config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import fs from 'fs'
2+
import yaml from 'js-yaml'
3+
4+
export default Object.freeze(yaml.safeLoad(fs.readFileSync('config/brandable_css.yml')))

0 commit comments

Comments
 (0)