Skip to content

Commit

Permalink
feat: routines
Browse files Browse the repository at this point in the history
  • Loading branch information
kissxp committed Oct 7, 2021
1 parent 61f26d6 commit 40bc4f4
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 12 deletions.
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,39 @@ dist/*.jpg
const clearPath = require( "clear-path" );

clearPath("dist");
```
```

## Multiple clear-path routines

### Example: package.json

Chose either one of the configurations samples below.

```json
# package.json
{
"scripts": {
"clean": "clear-path --routine=all",
"clean:dist": "clear-path --routine=dist"
},
"clearpath": {
"routine": {
"all": [
"dist",
"public"
],
"dist": "dist",
}
}
}
```

### Example: clearpath.rc

```yml
routine:
all:
dist
public
dist: dist
```
63 changes: 54 additions & 9 deletions bin/clear-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,37 @@ const filterConfig = ( config ) => {
return config;
}

const displayError = ( title ) => {
info( chalk.red( ">" ), chalk.bold.red(title) );
info( chalk.gray( "> read https://github.com/vkiss/clear-path/blob/main/README.md#configuration to learn how to configure clear-path.js" ) );
}

const getRoutine = ( args ) => {
for ( const arg of args ) {
if ( arg.split("=")[0] === "--routine" ) {
return arg.split("=")[1];
}
}

return undefined;
}

const clearPathArrayOrString = ( pathsToDelete ) => {
if ( typeof( pathsToDelete ) === "string" ) {
clearPath( pathsToDelete );
return;
}

if ( Array.isArray( pathsToDelete ) ) {
for ( const path of pathsToDelete ) {
clearPath(path)
}
return;
}

return displayError( "wrong configuration" );
}

const runScript = () => {
// Script start message
info( chalk.gray( "> clear-path.js" ) );
Expand All @@ -27,20 +58,34 @@ const runScript = () => {
explorer.search()
.then( ( result ) => {
if ( result === null ) {
info( chalk.red( ">" ), chalk.bold.red("configuration missing") );
info( chalk.gray( "> read https://github.com/vkiss/clear-path/blob/main/README.md#configuration to learn how to configure clear-path.js" ) );
return;
return displayError( "configuration missing" );
}

const pathsToDelete = filterConfig( result.config );

if ( typeof( pathsToDelete ) === "string" ) {
clearPath( pathsToDelete );
return;
}
if ( pathsToDelete.routine ) {
const args = process.argv.slice(2);
const argRoutine = getRoutine(args);

if ( argRoutine ) {
const routineToRun = pathsToDelete.routine[argRoutine];

for ( const path of pathsToDelete ) {
clearPath(path)
if ( routineToRun ) {
return clearPathArrayOrString( routineToRun );
}

if ( pathsToDelete.routine.default ) {
return clearPathArrayOrString( pathsToDelete.routine.default );
}

return displayError( `routine '${argRoutine}' not found` );
}

return displayError( "missing routine parameter" );
}

clearPathArrayOrString( pathsToDelete );

} )
.catch( ( error ) => {
info( chalk.red( error ) );
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "clear-path",
"version": "1.1.2",
"version": "1.2.0",
"description": "A node library for deleteing a path if exists using del",
"license": "MIT",
"repository": {
Expand Down Expand Up @@ -33,5 +33,9 @@
"cosmiconfig": "^7.0.1",
"del": "^6.0.0"
},
"clearpath": "dist"
"clearpath": {
"routine": {
"teste": "dist"
}
}
}

0 comments on commit 40bc4f4

Please sign in to comment.