-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: module and increase coverage (#5)
* chore: update deps * style: formatting * refactor: module refactor * test: increase coverage
- Loading branch information
1 parent
d807092
commit 43443c2
Showing
12 changed files
with
201 additions
and
134 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
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,3 @@ | ||
const consola = require('consola') | ||
|
||
module.exports = consola.withScope('nuxt:tailwindcss') |
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 |
---|---|---|
@@ -1,36 +1,26 @@ | ||
const { resolve } = require('path') | ||
const { pathExists, copy } = require('fs-extra') | ||
const logger = require('consola').withScope('@nuxtjs/tailwindcss') | ||
const logger = require('./logger') | ||
|
||
class ModuleUtils { | ||
constructor(nuxt, cwd) { | ||
this.nuxt = nuxt | ||
this.srcDir = nuxt.options.srcDir | ||
this.cwd = cwd | ||
} | ||
async function ensureTemplateFile(srcDir, from, to) { | ||
const relativePath = to.replace(srcDir, '~') | ||
const fileExists = await pathExists(to) | ||
|
||
resolveTemplate(path) { | ||
return resolve(this.cwd, 'templates', path) | ||
if (fileExists) { | ||
return true | ||
} | ||
|
||
async ensureTemplateFile(from, to) { | ||
const destPath = resolve(this.srcDir, to) | ||
const relativePath = destPath.replace(this.srcDir, '~') | ||
const fileExists = await pathExists(destPath) | ||
|
||
if (fileExists) { | ||
return true | ||
} | ||
try { | ||
// Copy docs: https://github.com/jprichardson/node-fs-extra/blob/master/docs/copy.md | ||
await copy(this.resolveTemplate(from), destPath) | ||
logger.success(`${relativePath} created`) | ||
return true | ||
} catch (err) { | ||
logger.warn(`Could not create ${relativePath}:`, err.message) | ||
return false | ||
} | ||
try { | ||
// Copy docs: https://github.com/jprichardson/node-fs-extra/blob/master/docs/copy.md | ||
await copy(resolve(__dirname, 'templates', from), to) | ||
logger.success(`${relativePath} created`) | ||
return true | ||
} catch (err) { | ||
logger.warn(`Could not create ${relativePath}:`, err.message) | ||
return false | ||
} | ||
} | ||
|
||
module.exports = ModuleUtils | ||
module.exports = { | ||
ensureTemplateFile | ||
} |
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,57 @@ | ||
jest.setTimeout(60000) | ||
|
||
const { join } = require('path') | ||
const { remove } = require('fs-extra') | ||
const { Nuxt, Builder } = require('nuxt-edge') | ||
const request = require('request-promise-native') | ||
const getPort = require('get-port') | ||
const logger = require('@/logger') | ||
|
||
const config = require('../example/nuxt.config') | ||
config.dev = false | ||
config.tailwindcss = { | ||
configPath: 'custom/tailwind.js', | ||
cssPath: 'custom/tailwind.css' | ||
} | ||
|
||
let nuxt, port | ||
|
||
const url = path => `http://localhost:${port}${path}` | ||
const get = path => request(url(path)) | ||
|
||
logger.mockTypes(() => jest.fn()) | ||
|
||
describe('config', () => { | ||
beforeAll(async () => { | ||
nuxt = new Nuxt(config) | ||
await nuxt.ready() | ||
await new Builder(nuxt).build() | ||
port = await getPort() | ||
await nuxt.listen(port) | ||
}) | ||
|
||
afterAll(async () => { | ||
await nuxt.close() | ||
await remove(join(nuxt.options.srcDir, 'custom')) | ||
}) | ||
|
||
beforeEach(() => { | ||
logger.clear() | ||
}) | ||
|
||
test('render', async () => { | ||
const html = await get('/') | ||
expect(html).toContain('Get the coolest t-shirts from our brand new store') | ||
expect(html).not.toContain('.bg-pink-700') | ||
}) | ||
|
||
test('custom paths', () => { | ||
expect(logger.success).toHaveBeenNthCalledWith(1, `~/${nuxt.options.tailwindcss.configPath} created`) | ||
expect(logger.success).toHaveBeenNthCalledWith(2, `~/${nuxt.options.tailwindcss.cssPath} created`) | ||
}) | ||
|
||
test('include custom tailwind.css file in project css', () => { | ||
expect(nuxt.options.css).toHaveLength(1) | ||
expect(nuxt.options.css).toContain(join(nuxt.options.srcDir, nuxt.options.tailwindcss.cssPath)) | ||
}) | ||
}) |
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,35 @@ | ||
jest.setTimeout(60000) | ||
require('fs-extra').pathExists = jest.fn().mockImplementation(() => Promise.resolve(false)) | ||
require('fs-extra').copy = jest.fn().mockImplementation(() => Promise.reject(new Error('Error when copy'))) | ||
|
||
const { Nuxt, Builder } = require('nuxt-edge') | ||
const logger = require('@/logger') | ||
|
||
const config = require('../example/nuxt.config') | ||
config.dev = false | ||
|
||
let nuxt | ||
|
||
logger.mockTypes(() => jest.fn()) | ||
|
||
describe('fail', () => { | ||
beforeAll(async () => { | ||
nuxt = new Nuxt(config) | ||
await nuxt.ready() | ||
await new Builder(nuxt).build() | ||
}) | ||
|
||
afterAll(async () => { | ||
await nuxt.close() | ||
}) | ||
|
||
beforeEach(() => { | ||
logger.clear() | ||
}) | ||
|
||
test('when copy files', () => { | ||
expect(logger.warn).toHaveBeenNthCalledWith(1, `Could not create ~/tailwind.config.js:`, 'Error when copy') | ||
expect(logger.warn).toHaveBeenNthCalledWith(2, `Could not create ~/assets/css/tailwind.css:`, 'Error when copy') | ||
expect(nuxt.options.css).toHaveLength(0) | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.