Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve clean task #130

Merged
merged 4 commits into from
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Gro changelog

## 0.8.5

- improve the [clean task](https://github.com/feltcoop/gro/blob/main/src/clean.task.ts)
([#130](https://github.com/feltcoop/gro/pull/130))
- running `gro clean` with no options behaves the same, deleting `/.gro/` and `/dist/`
- `gro clean` now accepts a number of options:
- `-s`: delete `/.svelte/`
- `-n`: delete `/node_modules/`
- `-B`: preserve `/.gro/`, the Gro build directory
- `-D`: preserve `/dist/`

## 0.8.4

- add `src/version.task.ts` to automate versioning and publishing
Expand Down
16 changes: 13 additions & 3 deletions src/clean.task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@ import type {Task} from './task/task.js';
import {clean} from './project/clean.js';

export const task: Task = {
description: 'remove build and temp files',
run: async ({log}): Promise<void> => {
await clean(log);
description:
'remove files: build/ (unless -B), dist/ (unless -D), and optionally .svelte/ (-s) and node_modules/ (-n)',
run: async ({log, args}): Promise<void> => {
// TODO document with mdsvex
await clean(
{
build: !args.B,
dist: !args.D,
svelteKit: !!args.s,
nodeModules: !!args.n,
},
log,
);
},
};
4 changes: 2 additions & 2 deletions src/dist.task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {copy} from './fs/nodeFs.js';
import {paths, toBuildOutPath} from './paths.js';
import {isTestBuildFile, isTestBuildArtifact} from './fs/testModule.js';
import {printPath} from './utils/print.js';
import {cleanDist} from './project/clean.js';
import {clean} from './project/clean.js';
import {loadGroConfig} from './config/config.js';
import {configureLogLevel} from './utils/log.js';
import {printBuildConfig} from './config/buildConfig.js';
Expand All @@ -16,7 +16,7 @@ export const task: Task = {
run: async ({log}) => {
const dev = process.env.NODE_ENV !== 'production';

await cleanDist(log);
await clean({dist: true}, log);

// This reads the `dist` flag on the build configs to help construct the final dist directory.
// See the docs at `./docs/config.md`.
Expand Down
2 changes: 1 addition & 1 deletion src/docs/tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ What is a task? See [`src/tasks/README.md`](../task).
- [build](../build.task.ts) - build the project
- [cert](../cert.task.ts) - creates a self-signed cert for https with openssl
- [check](../check.task.ts) - check that everything is ready to commit
- [clean](../clean.task.ts) - remove build and temp files
- [clean](../clean.task.ts) - remove files: build/ (unless -B), dist/ (unless -D), and optionally .svelte/ (-s) and node_modules/ (-n)
- [deploy](../deploy.task.ts) - deploy to gh-pages
- [dev](../dev.task.ts) - start dev server
- [dist](../dist.task.ts) - create the distribution
Expand Down
3 changes: 3 additions & 0 deletions src/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export const CONFIG_BUILD_BASE_PATH = 'gro.config.js';
export const EXTERNALS_BUILD_DIR = 'externals'; // TODO breaks the above trailing slash convention - revisit with trailing-slash branch
export const EXTERNALS_BUILD_DIR_SUBPATH = `/${EXTERNALS_BUILD_DIR}/`;

export const NODE_MODULES_PATH = 'node_modules';
export const SVELTE_KIT_PATH = '.svelte';

export interface Paths {
root: string;
source: string;
Expand Down
37 changes: 20 additions & 17 deletions src/project/clean.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import {pathExists, remove} from '../fs/nodeFs.js';
import {paths} from '../paths.js';
import {NODE_MODULES_PATH, paths, SVELTE_KIT_PATH} from '../paths.js';
import type {SystemLogger} from '../utils/log.js';
import {printPath} from '../utils/print.js';

export const clean = async (log: SystemLogger) => {
await cleanBuild(log);
await cleanDist(log);
};

// Checking `pathExists` avoids creating the directory if it doesn't exist.
export const cleanBuild = async (log: SystemLogger) => {
if (await pathExists(paths.build)) {
log.info('removing', printPath(paths.build));
await remove(paths.build);
}
};
export const clean = async (
{
build = false,
dist = false,
svelteKit = false,
nodeModules = false,
}: {build?: boolean; dist?: boolean; svelteKit?: boolean; nodeModules?: boolean},
log: SystemLogger,
) =>
Promise.all([
build ? cleanDir(paths.build, log) : null,
dist ? cleanDir(paths.dist, log) : null,
svelteKit ? cleanDir(SVELTE_KIT_PATH, log) : null,
nodeModules ? cleanDir(NODE_MODULES_PATH, log) : null,
]);

export const cleanDist = async (log: SystemLogger) => {
if (await pathExists(paths.dist)) {
log.info('removing', printPath(paths.dist));
await remove(paths.dist);
export const cleanDir = async (path: string, log: SystemLogger): Promise<void> => {
if (await pathExists(path)) {
log.info('removing', printPath(path));
await remove(path);
}
};