Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jaywcjlove/compile-less
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.4.0
Choose a base ref
...
head repository: jaywcjlove/compile-less
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.5.0
Choose a head ref
  • 15 commits
  • 10 files changed
  • 2 contributors

Commits on Mar 2, 2020

  1. Copy the full SHA
    57d5b27 View commit details
  2. Create LICENSE

    jaywcjlove authored Mar 2, 2020
    Copy the full SHA
    9464bc5 View commit details

Commits on Apr 25, 2020

  1. chore(deps): add renovate.json

    renovate-bot authored and jaywcjlove committed Apr 25, 2020
    Copy the full SHA
    a3e37da View commit details
  2. chore(deps): pin dependencies

    renovate-bot authored and jaywcjlove committed Apr 25, 2020
    Copy the full SHA
    392b636 View commit details

Commits on Aug 25, 2020

  1. Copy the full SHA
    5fe7655 View commit details
  2. Copy the full SHA
    a96d832 View commit details
  3. Copy the full SHA
    11c4de2 View commit details
  4. Copy the full SHA
    1257894 View commit details
  5. Copy the full SHA
    98c2b68 View commit details
  6. Copy the full SHA
    5cdcdc4 View commit details
  7. Copy the full SHA
    fbfe980 View commit details
  8. Copy the full SHA
    bf6cc94 View commit details
  9. Copy the full SHA
    fc1f0c8 View commit details
  10. Copy the full SHA
    61de211 View commit details
  11. released v1.5.0

    jaywcjlove committed Aug 25, 2020
    Copy the full SHA
    fbf5903 View commit details
Showing with 310 additions and 468 deletions.
  1. +17 −0 .github/workflows/ci.yml
  2. +21 −0 LICENSE
  3. +9 −6 README.md
  4. +161 −431 package-lock.json
  5. +26 −12 package.json
  6. +5 −0 renovate.json
  7. +50 −6 src/cli.ts
  8. +18 −10 src/compile.ts
  9. +1 −1 src/executeLess.ts
  10. +2 −2 src/getLessFiles.ts
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: GitHub Actions Deploy compile-less-cli
on:
push:
branches:
- master
jobs:
build-deploy:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '12'

- run: npm install
- run: npm run start
- run: npm run test
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 小弟调调™

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -34,13 +34,16 @@ Options:
-d, --dir <dir-path> Less file directory (default: "src")
-o, --out <dir-path> Output directory.
-c, --combine <file-name> Combine CSS files.
-w, --watch Watch and compile CSS files.
--exclude-css Exclude CSS file compilation.
-r, --rm-global Remove ":global" and ":global(className)".
-h, --help output usage information
-h, --help display help for command

Examples:
Examples:

$ compile-less -d src -o out
$ compile-less -d src -o out --rm-global
$ compile-less -d src -o css
$ compile-less -d src -o css --watch
$ compile-less -d src -o css --watch --exclude-css
$ compile-less -d src -o out --combine out/dist.css
```

@@ -49,8 +52,8 @@ Options:
Listen for files compiled with TypeScript

```bash
npm run dev
npm run compile
npm run start
npm run build
```

```bash
592 changes: 161 additions & 431 deletions package-lock.json

Large diffs are not rendered by default.

38 changes: 26 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
{
"name": "compile-less-cli",
"version": "1.4.0",
"version": "1.5.0",
"description": "compile less",
"main": "lib/compile.js",
"bin": {
"compile-less": "lib/cli.js"
},
"scripts": {
"prepack": "npm run start",
"dev": "npm run start -- --watch",
"start": "tsc --removeComments",
"test": "node ./lib/cli.js -d test/src -o test/out --combine test/out/dist.css"
"start": "npm run start -- --watch",
"build": "tsc --removeComments",
"test": "node ./lib/cli.js -d test/src -o test/out",
"test:watch": "node ./lib/cli.js -d test/src -o test/out --combine test/out/dist.css --watch",
"test:watch2": "node ./lib/cli.js -d test/src -o test/out --watch --exclude-css"
},
"repository": {
"type": "git",
@@ -22,18 +24,30 @@
"url": "https://github.com/jaywcjlove/compile-less/issues"
},
"homepage": "https://github.com/jaywcjlove/compile-less#readme",
"keywords": [
"less",
"css",
"style",
"cli",
"compile"
],
"files": [
"lib",
"src"
],
"devDependencies": {
"@types/fs-extra": "5.0.5",
"@types/less": "^3.0.1",
"@types/node": "11.13.0",
"typescript": "^3.8.3"
"@types/fs-extra": "9.0.1",
"@types/less": "3.0.1",
"@types/node": "14.6.0",
"typescript": "4.0.2"
},
"dependencies": {
"commander": "^4.1.1",
"chokidar": "3.4.2",
"commander": "^6.0.0",
"directory-tree": "^2.2.4",
"fs-extra": "^8.1.0",
"less": "^3.11.1",
"fs-extra": "^9.0.1",
"less": "^3.12.2",
"less-plugin-autoprefix": "2.0.0",
"tslib": "^1.11.1"
"tslib": "^2.0.1"
}
}
5 changes: 5 additions & 0 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": [
"config:base"
]
}
56 changes: 50 additions & 6 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#!/usr/bin/env node

import program from 'commander';
import compile from './compile';
import path from 'path';
import fs from 'fs';
import chokidar from 'chokidar';
import { executeLess } from './executeLess';
import compile, {outputFile} from './compile';

const logs = console.log;

@@ -15,12 +19,16 @@ program
.option('-d, --dir <dir-path>', 'Less file directory', 'src')
.option('-o, --out <dir-path>', 'Output directory.')
.option('-c, --combine <file-name>', 'Combine CSS files.')
.option('-w, --watch', 'Watch and compile CSS files.')
.option('--exclude-css', 'Exclude CSS file compilation.')
.option('-r, --rm-global', 'Remove ":global" and ":global(className)".')

program.on('--help', () => {
logs('\nExamples:');
logs();
logs(' $ compile-less -d src -o css');
logs(' $ compile-less -d src -o css --watch');
logs(' $ compile-less -d src -o css --watch --exclude-css');
logs(' $ compile-less -d src -o out --combine out/dist.css');
logs();
logs();
@@ -31,9 +39,45 @@ program.parse(process.argv);
if (!program.dir || (!program.out && !program.combine)) {
program.outputHelp();
} else {
compile(program.dir, {
out: program.out,
combine: program.combine,
rmGlobal: program.rmGlobal,
});
const inputDir = path.join(process.cwd(), program.dir);
if (program.watch) {
chokidar.watch(inputDir).on('all', chokidarWatch).on('ready', () => {
compile(program.dir, {
out: program.out,
combine: program.combine,
rmGlobal: program.rmGlobal,
});
console.log('Watching for file changes....');
});

} else {
compile(program.dir, {
out: program.out,
combine: program.combine,
rmGlobal: program.rmGlobal,
});
}
}

async function chokidarWatch(eventName: 'add'|'addDir'|'change'|'unlink'|'unlinkDir', changePath: string, stats?: fs.Stats) {
try {
const reg = program.excludeCss ? /\.(less)$/ : /\.(less|css)$/;
if (eventName === 'change' && reg.test(changePath)){
if (program.combine) {
return compile(program.dir, {
out: program.out,
combine: program.combine,
rmGlobal: program.rmGlobal,
});
}
const lessSource = await executeLess(changePath, program.rmGlobal);
outputFile(
lessSource,
path.join(process.cwd(), program.dir),
path.join(process.cwd(), program.out)
);
}
} catch (error) {
console.log('CLI:ERR:', error);
}
}
28 changes: 18 additions & 10 deletions src/compile.ts
Original file line number Diff line number Diff line change
@@ -8,12 +8,13 @@ export interface ICompileOtion {
out: string;
combine?: string;
rmGlobal?: boolean;
excludeCss?: boolean;
}

export default async function compile(dir: string, option: ICompileOtion) {
const inputDir = path.join(process.cwd(), dir);
try {
const files: Array<string> = await getLessFiles(inputDir);
const files: Array<string> = await getLessFiles(inputDir, option.excludeCss ? /\.(less)$/ : undefined);
const lessSource = await Promise.all(files.map(async (lessPath: string) => {
return executeLess(lessPath, option.rmGlobal);
}));
@@ -26,18 +27,25 @@ export default async function compile(dir: string, option: ICompileOtion) {
} else {
const outputDir = path.join(process.cwd(), option.out);
await Promise.all(lessSource.map(async (item: IOutputFile) => {
const logPathIn = item.path.replace(process.cwd(), '');
item.path = item.path.replace(inputDir, outputDir).replace(/.less$/, '.css');
const logPathOut = item.path.replace(process.cwd(), '');
console.log('log:', logPathIn, '->', logPathOut);
await fs.outputFile(item.path, item.css);
if (item.imports && item.imports.length > 0) {
console.log('imports:', item.imports);
}
return item;
return outputFile(item, inputDir, outputDir);
}));
}
} catch (error) {
console.log('error:', error);
}
}

export async function outputFile(data: IOutputFile, inputDir: string, outputDir: string) {
try {
const logPathIn = data.path.replace(process.cwd(), '');
data.path = data.path.replace(inputDir, outputDir).replace(/.less$/, '.css');
const logPathOut = data.path.replace(process.cwd(), '');
console.log('log:', logPathIn, '->', logPathOut);
await fs.outputFile(data.path, data.css);
if (data.imports && data.imports.length > 0) {
console.log('imports:', data.imports);
}
} catch (error) {
throw error;
}
}
2 changes: 1 addition & 1 deletion src/executeLess.ts
Original file line number Diff line number Diff line change
@@ -39,7 +39,7 @@ export function execute(command: string) {
});
}

export function executeLess(lessPath: string, rmGlobal?: boolean) {
export function executeLess(lessPath: string, rmGlobal?: boolean): Promise<IOutputFile> {
const lessStr = fs.readFileSync(lessPath);
return new Promise((resolve, reject) => {
less.render(lessStr.toString(), {
4 changes: 2 additions & 2 deletions src/getLessFiles.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import dirTree from 'directory-tree';

export function getLessFiles(folder: string) {
export function getLessFiles(folder: string, reg = /\.(less|css)$/) {
return new Promise<Array<string>>((resolve, reject) => {
const lessPaths: Array<string> = [];
dirTree(folder, {
extensions: /\.(less)$/
extensions: reg
}, (item) => {
lessPaths.push(item.path);
});