diff --git a/.eslintignore b/.eslintignore index ed9f9cc..d7256cb 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1 +1,6 @@ -coverage \ No newline at end of file +tap-snapshots +node_modules +modules +utils +dist +tmp \ No newline at end of file diff --git a/.eslintrc b/.eslintrc index 99325c3..c462614 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,18 +1,16 @@ { - "extends": [ - "airbnb-base", - "prettier" - ], - "plugins": [ - "prettier" - ], + "env": { + "es6": true, + "node": true, + "browser": false + }, + "parserOptions": { + "ecmaVersion": 2020 + }, + "extends": "airbnb-base", "rules": { - "strict": [ - 0, - "global" - ], - "class-methods-use-this": [ - 0 - ] + "indent": [1, 4], + "import/prefer-default-export": [0], + "import/extensions": [0] } -} \ No newline at end of file +} diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index edb17e6..f4a987e 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -1,50 +1,56 @@ name: Release and Publish on: - push: - branches: - - master - - alpha - - beta - - next + push: + branches: + - master + - alpha + - beta + - next jobs: - test: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v2 - - name: Setup Node.js - uses: actions/setup-node@v2 - with: - node-version: 14.x - - name: npm install - run: | - npm install - - name: npm lint - run: | - npm run lint - - name: npm test - run: | - npm run test:ci + test: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Setup Node.js + uses: actions/setup-node@v2 + with: + node-version: 14.x + - name: npm install + run: | + npm install + env: + CI: true + - name: npm run lint + run: | + npm run lint + env: + CI: true + - name: npm test + run: | + npm test + env: + CI: true - release: - name: Release - runs-on: ubuntu-latest - needs: [test] - steps: - - name: Checkout - uses: actions/checkout@v2 - - name: Setup Node.js - uses: actions/setup-node@v2 - with: - node-version: 14.x - - name: npm install - run: | - npm install - - name: npx semantic-release - run: | - npx semantic-release - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + release: + name: Release + runs-on: ubuntu-latest + needs: [test] + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Setup Node.js + uses: actions/setup-node@v2 + with: + node-version: 14.x + - name: npm install + run: | + npm install + - name: npx semantic-release + run: | + npx semantic-release + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7cb7820..5ebd9fd 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,11 +1,6 @@ name: Run Lint and Tests -on: - push: - branches-ignore: - - master - - alpha - - beta +on: push jobs: build: @@ -23,15 +18,9 @@ jobs: - name: npm install run: | npm install - env: - CI: true - name: npm lint run: | npm run lint - env: - CI: true - - name: npm test + - name: Run tests run: | - npm run test:ci - env: - CI: true + npm test diff --git a/index.js b/index.js index ebff57a..f389cb6 100644 --- a/index.js +++ b/index.js @@ -1,140 +1,58 @@ -'use strict'; - -const { readFileSync } = require('fs'); -const { join } = require('path'); -const pkgDir = require('pkg-dir'); -const { AssetJs, AssetCss } = require('@podium/utils'); -const { schemas } = require('@eik/common'); - -const scripts = Symbol('assets:scripts'); -const styles = Symbol('assets:styles'); - -function validateMeta(meta) { - const { value, error } = schemas.assets(meta); - - if (error) { - throw new Error(error); +import { helpers } from '@eik/common'; +import { join } from 'path'; + +const isUrl = (value = '') => value.startsWith('http'); + +export default class EikNodeClient { + constructor({ + development = false, + base = '', + path = process.cwd(), + + } = {}) { + this.pDevelopment = development; + this.pConfig = {}; + this.pPath = path; + this.pBase = base; } - return value; -} - -function readAssetsJson(path) { - const metaPath = join(pkgDir.sync(), path); - const metaString = readFileSync(metaPath, 'utf8'); - return validateMeta(JSON.parse(metaString)); -} - -module.exports = class Client { - constructor({ js, css, development = false, path = './assets.json' }) { - const meta = readAssetsJson(path); - - const { - server, - js: { input: jsInput, options: jsOptions }, - css: { input: cssInput, options: cssOptions }, - organisation, - name, - version, - } = meta; - this[scripts] = []; - this[styles] = []; - - if (development) { - if (js) { - let script = {}; - if (typeof js !== 'string') { - script = new AssetJs(js); - } else { - script = new AssetJs({ - type: 'module', - value: js, - ...jsOptions, - }); - } - - this[scripts].push(script); - } - if (css) { - let style = {}; - if (typeof css !== 'string') { - style = new AssetCss(css); - } else { - style = new AssetCss({ value: css, ...cssOptions }); - } + async load() { + this.pConfig = await helpers.getDefaults(this.pPath); + } - this[styles].push(style); - } - return; - } + get name() { + if (this.pConfig.name) return this.pConfig.name; + return ''; + } - if (jsInput) { - this[scripts].push( - new AssetJs({ - type: 'module', - ...jsOptions, - value: - server + - join( - '/', - organisation, - 'pkg', - name, - version, - `/main/index.js`, - ), - }), - ); - this[scripts].push( - new AssetJs({ - ...jsOptions, - type: 'iife', - nomodule: true, - value: - server + - join( - '/', - organisation, - 'pkg', - name, - version, - `/ie11/index.js`, - ), - }), - ); - } - if (cssInput) { - this[styles].push( - new AssetCss({ - ...cssOptions, - value: - server + - join( - '/', - organisation, - 'pkg', - name, - version, - `/main/index.css`, - ), - }), - ); - } + get version() { + if (this.pConfig.version) return this.pConfig.version; + return ''; } - get js() { - return this[scripts]; + get type() { + if (this.pConfig.type && this.pConfig.type === 'package') return 'pkg'; + if (this.pConfig.type) return this.pConfig.type; + return ''; } - get css() { - return this[styles]; + get server() { + if (this.pConfig.server) return this.pConfig.server; + return ''; } - get scripts() { - return this.js.map(s => s.toHTML()).join('\n'); + get pathname() { + return join('/', this.type, this.name, this.version); } - get styles() { - return this.css.map(s => s.toHTML()).join('\n'); + file(file = '') { + if (this.pDevelopment) { + if (isUrl(this.pBase)) { + const base = new URL(this.pBase); + return new URL(join(base.pathname, file), base).href; + } + return join(this.pBase, file); + } + return new URL(join(this.pathname, file), this.server).href; } -}; +} diff --git a/package.json b/package.json index aaf4bb3..630fc18 100644 --- a/package.json +++ b/package.json @@ -2,19 +2,19 @@ "name": "@eik/client", "version": "1.0.0-alpha.1", "description": "Node.js client for Eik", + "type": "module", "main": "index.js", "scripts": { - "test": "tap", - "lint:format": "eslint --fix .", + "test": "tap --no-esm test/*.js --no-coverage", + "lint:fix": "eslint --fix .", "lint": "eslint ." }, "keywords": [], "author": "", - "license": "ISC", + "license": "MIT", "dependencies": { - "@eik/common": "^1.0.0-alpha.1", - "@podium/utils": "^4.1.2", - "pkg-dir": "^4.2.0" + "@eik/common": "4.0.0-next.4", + "abslog": "2.4.0" }, "devDependencies": { "@semantic-release/changelog": "5.0.1", @@ -22,14 +22,14 @@ "@semantic-release/git": "9.0.0", "@semantic-release/github": "7.2.0", "@semantic-release/npm": "7.0.10", - "@semantic-release/release-notes-generator": "9.0.1", - "eslint": "6.6.0", - "eslint-config-airbnb-base": "14.0.0", - "eslint-config-prettier": "6.6.0", - "eslint-plugin-import": "2.18.2", - "eslint-plugin-prettier": "3.1.1", - "prettier": "1.19.1", + "@semantic-release/release-notes-generator": "9.0.2", + "eslint": "7.21.0", + "eslint-config-airbnb-base": "14.2.1", + "eslint-config-prettier": "8.1.0", + "eslint-plugin-import": "2.22.1", + "eslint-plugin-prettier": "3.3.1", + "prettier": "2.2.1", "semantic-release": "17.4.1", - "tap": "14.9.2" + "tap": "14.11.0" } } diff --git a/release.config.js b/release.config.js index b9b3afc..f195d37 100644 --- a/release.config.js +++ b/release.config.js @@ -1,27 +1,27 @@ module.exports = { - plugins: [ - '@semantic-release/commit-analyzer', - '@semantic-release/release-notes-generator', - '@semantic-release/changelog', - [ - '@semantic-release/npm', - { - tarballDir: 'release', - }, + plugins: [ + '@semantic-release/commit-analyzer', + '@semantic-release/release-notes-generator', + '@semantic-release/changelog', + [ + '@semantic-release/npm', + { + tarballDir: 'release', + }, + ], + [ + '@semantic-release/github', + { + assets: 'release/*.tgz', + }, + ], + '@semantic-release/git', ], - [ - '@semantic-release/github', - { - assets: 'release/*.tgz', - }, + preset: 'angular', + branches: [ + { name: 'master' }, + { name: 'alpha', prerelease: true }, + { name: 'beta', prerelease: true }, + { name: 'next', prerelease: true }, ], - '@semantic-release/git', - ], - preset: 'angular', - branches: [ - { name: 'master' }, - { name: 'alpha', prerelease: true }, - { name: 'beta', prerelease: true }, - { name: 'next', prerelease: true }, - ], }; diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..d7116d2 --- /dev/null +++ b/test/index.js @@ -0,0 +1,13 @@ +import tap from 'tap'; +import EikNodeClient from '../index.js'; + +tap.test('test', async (t) => { + const client = new EikNodeClient({ + development: false, + base: '/public', + }); + await client.load(); + + t.same('X', 'X'); + t.end(); +}); diff --git a/test/index.test.js b/test/index.test.js deleted file mode 100644 index ccf0fa0..0000000 --- a/test/index.test.js +++ /dev/null @@ -1,246 +0,0 @@ -'use strict'; - -const { test } = require('tap'); -const Client = require('../'); - -test('development mode true - relative paths', t => { - const c = new Client({ - js: '/assets/scripts.js', - css: '/assets/styles.css', - path: './test/mocks/assets-1.json', - development: true, - }); - - t.same( - JSON.parse(JSON.stringify(c.js)), - [ - { - type: 'module', - value: '/assets/scripts.js', - }, - ], - 'client.js should return JS assets object', - ); - t.same( - JSON.parse(JSON.stringify(c.css)), - [ - { - type: 'text/css', - value: '/assets/styles.css', - rel: 'stylesheet', - }, - ], - 'client.css should return CSS assets object', - ); - t.end(); -}); - -test('development mode true - absolute paths', t => { - const c = new Client({ - js: 'http://my-dev-assets/scripts.js', - css: 'http://my-dev-assets/styles.css', - path: './test/mocks/assets-1.json', - development: true, - }); - - t.same( - JSON.parse(JSON.stringify(c.js)), - [ - { - type: 'module', - value: 'http://my-dev-assets/scripts.js', - }, - ], - 'client.js should return JS assets object', - ); - t.same( - JSON.parse(JSON.stringify(c.css)), - [ - { - type: 'text/css', - value: 'http://my-dev-assets/styles.css', - rel: 'stylesheet', - }, - ], - 'client.css should return CSS assets object', - ); - t.end(); -}); - -test('development mode true, setting options from assets.json', t => { - const c = new Client({ - js: 'http://my-dev-assets/scripts.js', - css: 'http://my-dev-assets/styles.css', - path: './test/mocks/assets-2.json', - development: true, - }); - - t.same( - JSON.parse(JSON.stringify(c.js)), - [ - { - value: 'http://my-dev-assets/scripts.js', - type: 'module', - defer: true, - }, - ], - 'client.js should return JS assets object', - ); - t.same( - JSON.parse(JSON.stringify(c.css)), - [ - { - value: 'http://my-dev-assets/styles.css', - type: 'text/css', - rel: 'stylesheet', - }, - ], - 'client.css should return CSS assets object', - ); - t.end(); -}); - -test('development mode true, overriding options', t => { - const c = new Client({ - js: { - value: 'http://my-dev-assets/scripts.js', - type: 'iife', - async: true, - defer: true, - }, - css: { - value: 'http://my-dev-assets/styles.css', - type: 'text/css', - title: 'some title', - }, - path: './test/mocks/assets-2.json', - development: true, - }); - - t.same( - JSON.parse(JSON.stringify(c.js)), - [ - { - value: 'http://my-dev-assets/scripts.js', - type: 'iife', - defer: true, - async: true, - }, - ], - 'client.js should return JS assets object', - ); - t.same( - JSON.parse(JSON.stringify(c.css)), - [ - { - value: 'http://my-dev-assets/styles.css', - type: 'text/css', - rel: 'stylesheet', - title: 'some title', - }, - ], - 'client.css should return CSS assets object', - ); - t.end(); -}); - -test('development mode false', t => { - const c = new Client({ - path: './test/mocks/assets-1.json', - development: false, - }); - - t.same( - JSON.parse(JSON.stringify(c.js)), - [ - { - type: 'module', - value: - 'http://localhost:4001/my-org/pkg/my-app-name/1.0.0/main/index.js', - }, - { - type: 'iife', - value: - 'http://localhost:4001/my-org/pkg/my-app-name/1.0.0/ie11/index.js', - nomodule: true, - }, - ], - 'client.js should return JS assets object', - ); - t.same( - JSON.parse(JSON.stringify(c.css)), - [ - { - type: 'text/css', - value: - 'http://localhost:4001/my-org/pkg/my-app-name/1.0.0/main/index.css', - rel: 'stylesheet', - }, - ], - 'client.css should return CSS assets object', - ); - t.end(); -}); - -test('development mode false, setting options', t => { - const c = new Client({ - path: './test/mocks/assets-3.json', - development: false, - }); - - t.same( - JSON.parse(JSON.stringify(c.js)), - [ - { - value: - 'http://localhost:4001/my-org/pkg/my-app-name/1.0.0/main/index.js', - async: true, - defer: true, - type: 'module', - }, - { - value: - 'http://localhost:4001/my-org/pkg/my-app-name/1.0.0/ie11/index.js', - async: true, - defer: true, - type: 'iife', - nomodule: true, - }, - ], - 'client.js should return JS assets object', - ); - t.same( - JSON.parse(JSON.stringify(c.css)), - [ - { - title: 'my title', - value: - 'http://localhost:4001/my-org/pkg/my-app-name/1.0.0/main/index.css', - type: 'text/css', - rel: 'stylesheet', - }, - ], - 'client.css should return CSS assets object', - ); - t.end(); -}); - -test('development mode false, scripts and styles', t => { - const c = new Client({ - path: './test/mocks/assets-1.json', - development: false, - }); - - t.same( - JSON.parse(JSON.stringify(c.scripts)), - ` -`, - 'client.scripts should return JS scripts markup', - ); - t.same( - JSON.parse(JSON.stringify(c.styles)), - ``, - 'client.styles should return CSS styles markup', - ); - t.end(); -});