Skip to content

Commit

Permalink
Add dependency finder package
Browse files Browse the repository at this point in the history
  • Loading branch information
noahtallen committed May 24, 2021
1 parent ff23ee9 commit a7c144f
Show file tree
Hide file tree
Showing 12 changed files with 816 additions and 36 deletions.
2 changes: 1 addition & 1 deletion client/lib/plans/features-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {
FEATURE_BLANK,
FEATURE_BLOG_DOMAIN,
FEATURE_BUSINESS_ONBOARDING,
FEATURE_CLOUDFLARE_ANALYTICS,
FEATURE_CLOUDFLARE_ANALYTICS,
FEATURE_COLLECT_PAYMENTS_V2,
FEATURE_COMMUNITY_SUPPORT,
FEATURE_CRM_LEADS_AND_FUNNEL,
Expand Down
90 changes: 90 additions & 0 deletions package-map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
[
{ "path": "packages/accessible-focus" },
{ "path": "packages/accessible-focus" },
{ "path": "packages/babel-plugin-i18n-calypso" },
{ "path": "packages/babel-plugin-transform-wpcalypso-async" },
{ "path": "packages/browser-data-collector" },
{ "path": "packages/calypso-analytics" },
{ "path": "packages/calypso-build" },
{ "path": "packages/calypso-codemods" },
{ "path": "packages/calypso-color-schemes" },
{ "path": "packages/calypso-config" },
{ "path": "packages/calypso-doctor" },
{ "path": "packages/calypso-polyfills" },
{ "path": "packages/calypso-stripe" },
{ "path": "packages/components" },
{ "path": "packages/composite-checkout" },
{ "path": "packages/create-calypso-config" },
{ "path": "packages/data-stores" },
{ "path": "packages/dependency-finder" },
{ "path": "packages/domain-picker" },
{ "path": "packages/effective-module-tree" },
{ "path": "packages/eslint-plugin-wpcalypso" },
{ "path": "packages/explat-client-react-helpers" },
{ "path": "packages/explat-client" },
{ "path": "packages/format-currency" },
{ "path": "packages/i18n-calypso-cli" },
{ "path": "packages/i18n-calypso" },
{ "path": "packages/i18n-utils" },
{ "path": "packages/js-utils" },
{ "path": "packages/language-picker" },
{ "path": "packages/languages" },
{ "path": "packages/launch" },
{ "path": "packages/load-script" },
{ "path": "packages/material-design-icons" },
{ "path": "packages/onboarding" },
{ "path": "packages/page-pattern-modal" },
{ "path": "packages/photon" },
{ "path": "packages/plans-grid" },
{ "path": "packages/popup-monitor" },
{ "path": "packages/request-external-access" },
{ "path": "packages/retarget-open-prs" },
{ "path": "packages/search" },
{ "path": "packages/shopping-cart" },
{ "path": "packages/social-previews" },
{ "path": "packages/spec-junit-reporter" },
{ "path": "packages/spec-xunit-reporter" },
{ "path": "packages/state-utils" },
{ "path": "packages/tree-select" },
{ "path": "packages/typography" },
{ "path": "packages/viewport-react" },
{ "path": "packages/viewport" },
{ "path": "packages/webpack-config-flag-plugin" },
{ "path": "packages/webpack-extensive-lodash-replacement-plugin" },
{ "path": "packages/webpack-inline-constant-exports-plugin" },
{ "path": "packages/webpack-rtl-plugin" },
{ "path": "packages/whats-new" },
{ "path": "packages/wp-babel-makepot" },
{ "path": "packages/wpcom-checkout" },
{ "path": "packages/wpcom-proxy-request" },
{ "path": "packages/wpcom.js" },
{
"path": "apps/editing-toolkit",
"additionalEntryPoints": [
"apps/editing-toolkit/editing-toolkit-plugin/block-inserter-modifications/contextual-tips.js",
"apps/editing-toolkit/editing-toolkit-plugin/block-patterns/index.ts",
"apps/editing-toolkit/editing-toolkit-plugin/common/index.js",
"apps/editing-toolkit/editing-toolkit-plugin/common/data-stores/index.ts",
"apps/editing-toolkit/editing-toolkit-plugin/common/hide-plugin-buttons-mobile.js",
"apps/editing-toolkit/editing-toolkit-plugin/dotcom-fse/index.js",
"apps/editing-toolkit/editing-toolkit-plugin/editor-site-launch/focused-launch.ts",
"apps/editing-toolkit/editing-toolkit-plugin/editor-site-launch/gutenboarding-launch.ts",
"apps/editing-toolkit/editing-toolkit-plugin/editor-site-launch/launch-button.ts",
"apps/editing-toolkit/editing-toolkit-plugin/event-countdown-block/index.js",
"apps/editing-toolkit/editing-toolkit-plugin/global-styles/index.js",
"apps/editing-toolkit/editing-toolkit-plugin/jetpack-timeline/index.js",
"apps/editing-toolkit/editing-toolkit-plugin/newspack-blocks/blog-posts-block-editor.js",
"apps/editing-toolkit/editing-toolkit-plugin/newspack-blocks/blog-posts-block-view.js",
"apps/editing-toolkit/editing-toolkit-plugin/newspack-blocks/carousel-block-editor.js",
"apps/editing-toolkit/editing-toolkit-plugin/newspack-blocks/carousel-block-view.js",
"apps/editing-toolkit/editing-toolkit-plugin/posts-list-block.js",
"apps/editing-toolkit/editing-toolkit-plugin/starter-page-templates/index.tsx",
"apps/editing-toolkit/editing-toolkit-plugin/whats-new/index.js",
"apps/editing-toolkit/editing-toolkit-plugin/wpcom-block-editor-nav-sidebar/index.ts",
"apps/editing-toolkit/editing-toolkit-plugin/wpcom-block-editor-nux/index.js",
"apps/editing-toolkit/editing-toolkit-plugin/error-reporting/index.js",
"apps/editing-toolkit/editing-toolkit-plugin/posts-list-block/index.js",
"apps/editing-toolkit/editing-toolkit-plugin/**/*.php"
]
}
]
10 changes: 10 additions & 0 deletions packages/dependency-finder/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
env: {
node: true,
},
rules: {
// This is a node.js project, it is ok to import node modules
'import/no-nodejs-modules': 'off',
'no-console': 'off',
},
};
32 changes: 32 additions & 0 deletions packages/dependency-finder/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "@automattic/dependency-finder",
"version": "0.1.0",
"description": "Generate the list of dependencies of a set of files, recursively",
"main": "index.ts",
"author": "Automattic Inc.",
"type": "module",
"repository": {
"type": "git",
"url": "git+https://github.com/Automattic/wp-calypso.git",
"directory": "packages/dependency-finder"
},
"keywords": [
"dependencies",
"node_modules"
],
"license": "GPL-2.0-or-later",
"dependencies": {
"dependency-tree": "^8.1.0",
"globby": "^11.0.3",
"jest-config": "^26.6.3",
"read-pkg": "^6.0.0",
"read-pkg-up": "^8.0.0",
"tslib": "^2.2.0"
},
"scripts": {
"clean": "tsc --build ./tsconfig.json --clean && npx rimraf dist",
"build": "tsc --build ./tsconfig.json",
"prepack": "yarn run clean && yarn run build",
"watch": "tsc --build ./tsconfig.json --watch"
}
}
88 changes: 88 additions & 0 deletions packages/dependency-finder/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* External dependencies
*/
import path from 'path';
import fs from 'fs';
import { fileURLToPath } from 'url';
import childProcess from 'child_process';
import { promisify } from 'util';

const exec = promisify( childProcess.exec );

/**
* Internal dependencies
*/
import { findDependencies } from './lib/find-dependencies.js';

const packageMapPath = path.join(
path.dirname( fileURLToPath( import.meta.url ) ),
'../../../../package-map.json'
);

type PackageMapEntry = {
path: string;
additionalEntryPoints: Array< string > | undefined;
};
type PackageMap = Array< PackageMapEntry >;
const packageMap: PackageMap = JSON.parse( fs.readFileSync( packageMapPath, 'utf8' ) );

function getMonorepoPackages() {
const packages = fs.readdirSync( 'packages', { withFileTypes: true } );
return packages
.filter( ( entry ) => entry.isDirectory() )
.map( ( entry ) => path.resolve( 'packages', entry.name ) );
}
const monorepoPackages = getMonorepoPackages();

const findPackageDependencies = async ( {
path: pkgPath,
additionalEntryPoints,
}: PackageMapEntry ) => {
const absolutePkgPath = path.resolve( pkgPath );

const { missing, packages, modules } = await findDependencies( {
pkg: absolutePkgPath,
additionalEntryPoints,
monorepoPackages,
} );
const { stdout } = await exec(
`find ${ absolutePkgPath } -type f -not \\( -path '*/node_modules/*' -o -path '*/.cache/*' -o -path '*/dist/*' \\)`
);
const allFiles = stdout.trim().split( '\n' );

// Files which exist in the filesystem, but the dep finder did not parse.
// We exclude files which do not impact builds, such as ".txt" or ".md" files.
const unknownFiles = allFiles.filter(
( file ) => ! modules.includes( file ) && ! [ '.md', '.txt' ].includes( path.extname( file ) )
);

console.log( 'Package:' );
console.log( ' ' + pkgPath );
console.log( 'Missing files:' );
console.log( missing.length ? missing.map( ( m ) => ' ' + m ).join( '\n' ) : ' -' );
console.log( 'Packages:' );
console.log( packages.length ? packages.map( ( m ) => ' ' + m ).join( '\n' ) : ' -' );
console.log( 'Found files:' );
console.log( modules.length ? modules.map( ( m ) => ' ' + m ).join( '\n' ) : ' -' );
console.log( 'Unkown files:' );
console.log( unknownFiles.length ? unknownFiles.map( ( m ) => ' ' + m ).join( '\n' ) : ' -' );
console.log();
console.log();
};

const main = async () => {
const packageToParse = process.argv.slice( 2 )[ 0 ];
// Allow parsing a single package.
if ( packageToParse ) {
const packageEntry = packageMap.find( ( { path } ) => path === packageToParse );
if ( packageEntry ) {
findPackageDependencies( packageEntry );
}
} else {
for ( const packageEntry of packageMap ) {
await findPackageDependencies( packageEntry );
}
}
};

main().catch( console.error );
12 changes: 12 additions & 0 deletions packages/dependency-finder/src/lib/entrypoints/additional.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* External dependencies
*/
import globby from 'globby';

export const findAdditionalEntryPoints = async (
additionalEntryPoints: string[]
): Promise< string[] > => {
return (
await Promise.all( additionalEntryPoints.map( ( pattern ) => globby( pattern ) ) )
).flat();
};
23 changes: 23 additions & 0 deletions packages/dependency-finder/src/lib/entrypoints/jest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* External dependencies
*/
import path from 'path';
import jestConfig from 'jest-config';
import globby from 'globby';

export const findJestEntrypoints = async ( {
jestConfigPath,
}: {
jestConfigPath: string;
} ): Promise< string[] > => {
const config = await jestConfig.readConfig(
{
config: jestConfigPath,
_: [ '' ],
$0: '',
},
path.dirname( jestConfigPath )
);
const tests = await globby( config.projectConfig.testMatch );
return tests;
};
30 changes: 30 additions & 0 deletions packages/dependency-finder/src/lib/entrypoints/packagejson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* External dependencies
*/
import { readPackageAsync } from 'read-pkg';
import path from 'path';
import fs from 'fs';

export const findPackageJsonEntrypoints = async ( {
pkgPath,
}: {
pkgPath: string;
} ): Promise< string[] > => {
const pkgJson = await readPackageAsync( { cwd: pkgPath } );

const main = pkgJson[ 'calypso:src' ] ?? pkgJson.main ?? 'index.js';
const bin = pkgJson.bin ?? {};

const files = [ main, ...Object.values( bin ) ]
.filter( Boolean )
.map( ( file ) => path.resolve( pkgPath, file ) )
.filter( ( file ) => {
try {
fs.accessSync( file );
return true;
} catch {
return false;
}
} );
return files;
};
Loading

0 comments on commit a7c144f

Please sign in to comment.