Skip to content

Conversation

@trivikr
Copy link
Member

@trivikr trivikr commented Jan 10, 2022

Issue

Refs: #3166

Description

Adds npm script clean to all packages

Script used for adding clean
import { readdirSync, readFileSync, writeFileSync } from "fs";
import { join } from "path";

const workspaces = ["clients", "private", "packages", "lib"];

workspaces.forEach((workspacesDir) => {
  // Process each workspace in workspace directory
  readdirSync(join(process.cwd(), workspacesDir), { withFileTypes: true })
    .filter((dirent) => dirent.isDirectory())
    .map((dirent) => dirent.name)
    .forEach((workspaceDir) => {
      const cwd = join(process.cwd(), workspacesDir, workspaceDir);

      const packageJsonPath = join(cwd, "package.json");
      const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf8").toString());
      if (!packageJson["scripts"]["clean"]) {
        writeFileSync(
          packageJsonPath,
          JSON.stringify(
            {
              ...packageJson,
              scripts: Object.fromEntries(
                Object.entries({
                  ...packageJson.scripts,
                  clean: "rimraf ./dist-*",
                }).sort()
              ),
            },
            null,
            2
          ).concat(`\n`)
        );
      }
    });
});

Testing

Verified that running yarn clean deletes the dist-* folder with abort-controller package.

Output
$ abort-controller> ls dist-*
dist-cjs:
AbortController.js  AbortSignal.js  index.js

dist-es:
AbortController.js  AbortSignal.js  index.js

dist-types:
AbortController.d.ts  AbortSignal.d.ts  index.d.ts

$ abort-controller> yarn clean
yarn run v1.22.17
$ rimraf ./dist-*
Done in 0.10s.

$ abort-controller> ls dist-* 
zsh: no matches found: dist-*

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@trivikr trivikr requested a review from a team as a code owner January 10, 2022 15:30
@trivikr trivikr merged commit 370540a into aws:main Jan 10, 2022
@trivikr trivikr deleted the npm-clean-script branch January 10, 2022 17:46
@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants