Skip to content

Commit

Permalink
fix(infrastructure): types generated directory problem (material-com…
Browse files Browse the repository at this point in the history
  • Loading branch information
태재영 authored and Matt Goo committed Jun 25, 2019
1 parent bdb3f43 commit 3d61838
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 35 deletions.
1 change: 1 addition & 0 deletions packages/dialog/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "Material Components React Dialog",
"license": "MIT",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"keywords": [
"mdc web react",
"material components react",
Expand Down
18 changes: 13 additions & 5 deletions scripts/directory-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ const {basename, join, resolve} = require('path');

const denyList = ['images'];

const isDirectory = source => lstatSync(source).isDirectory();
const getDirectories = source =>
readdirSync(source).map(name => join(source, name)).filter(isDirectory);
const isDirectory = (source) => lstatSync(source).isDirectory();
const getDirectories = (source) =>
readdirSync(source)
.map((name) => join(source, name))
.filter(isDirectory);

function readScreenshotDirectory(
components = [],
Expand All @@ -20,9 +22,15 @@ function readScreenshotDirectory(
// recursively get sub directories
const subDirectories = getDirectories(resolve(path, packageName));
if (subDirectories.length > 0) {
readScreenshotDirectory(components, resolve(path, packageName), packageName);
readScreenshotDirectory(
components,
resolve(path, packageName),
packageName
);
}
components.push(`${parentDirectory ? parentDirectory + '/' : ''}${packageName}`);
components.push(
`${parentDirectory ? parentDirectory + '/' : ''}${packageName}`
);
});

return components;
Expand Down
6 changes: 3 additions & 3 deletions scripts/package-json-reader.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const fs = require('fs');
const {resolve, join} = require('path');
const packageJson = JSON.parse(fs.readFileSync(resolve(__dirname, '../package.json'), 'utf8'));

const packageJson = JSON.parse(
fs.readFileSync(resolve(__dirname, '../package.json'), 'utf8')
);

const readMaterialPackages = () => {
const dependencies = [
Expand All @@ -17,5 +18,4 @@ const readMaterialPackages = () => {
return dependencies;
};


module.exports = {readMaterialPackages};
5 changes: 3 additions & 2 deletions scripts/package-name-converter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const {basename} = require('path');

const dashedToCamel = (name) => name.replace(/-(\w)/g, (_, v) => v.toUpperCase());
const dashedToCamel = (name) =>
name.replace(/-(\w)/g, (_, v) => v.toUpperCase());

convertToImportMDCWebPaths = (packageNames) => {
return packageNames.map((packageName) => {
Expand All @@ -9,6 +10,6 @@ convertToImportMDCWebPaths = (packageNames) => {
// https://github.com/material-components/material-components-web/pull/3245
return `@material/${name}/dist/mdc.${dashedToCamel(name)}`;
});
}
};

module.exports = {convertToImportMDCWebPaths};
27 changes: 16 additions & 11 deletions scripts/release/cp-pkgs.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,15 @@ const {sync: globSync} = require('glob');

const PKG_RE = /^(([a-z]*\-?)*)/;

const isValidCwd = (
const isValidCwd =
path.basename(process.cwd()) === 'material-components-web-react' &&
fs.existsSync('packages') &&
fs.existsSync('build')
);
fs.existsSync('build');

if (!isValidCwd) {
console.error(
'Invalid CWD. Please ensure you are running this from the root of the repo, ' +
'and that you have run `npm run build`'
'and that you have run `npm run build`'
);
process.exit(0);
}
Expand All @@ -54,7 +53,9 @@ function getAssetEntry(asset) {
function cpAsset(asset) {
const assetPkg = path.join('packages', getAssetEntry(asset));
if (!fs.existsSync(assetPkg)) {
Promise.reject(new Error(`Non-existent asset package path ${assetPkg} for ${asset}`));
Promise.reject(
new Error(`Non-existent asset package path ${assetPkg} for ${asset}`)
);
}

let basename = path.basename(asset);
Expand All @@ -71,16 +72,20 @@ function cpAsset(asset) {
}

const destDir = path.join(assetPkg, 'dist', basename);
return cpFile(asset, destDir)
.then(() => console.log(`cp ${asset} -> ${destDir}`));
return cpFile(asset, destDir).then(() =>
console.log(`cp ${asset} -> ${destDir}`)
);
}

// this takes a file path to an index.d.ts file and adds an //@ts-ignore comment
// above the MDC Web imports (any line that includes `/dist/`). We need to ignore
// these lines since MDC Web does not have typing files
// TODO: https://github.com/material-components/material-components-web-react/issues/574
function addTsIgnore(filePath) {
const data = fs.readFileSync(filePath).toString().split('\n');
const data = fs
.readFileSync(filePath)
.toString()
.split('\n');
const lineNumber = data.findIndex((lineText) => lineText.includes('/dist/'));
if (lineNumber <= -1) return;

Expand All @@ -100,8 +105,9 @@ function cpTypes(typeAsset) {
destDir.splice(2, 0, 'dist');
destDir = `${destDir.join('/')}/${base}`;
addTsIgnore(typeAsset);
return cpFile(typeAsset, destDir)
.then(() => console.log(`cp ${typeAsset} -> ${destDir}`));
return cpFile(typeAsset, destDir).then(() =>
console.log(`cp ${typeAsset} -> ${destDir}`)
);
}

async function copyPackages() {
Expand All @@ -117,4 +123,3 @@ async function copyPackages() {
}

copyPackages();

53 changes: 41 additions & 12 deletions scripts/release/verify-pkg-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,37 +31,66 @@ const isValidCwd = fs.existsSync('packages');
if (!isValidCwd) {
console.error(
'Invalid CWD. Please ensure you are running this from the root of the repo, and that you have run ' +
'`npm run dist` and `node scripts/cp-pkgs.js`'
'`npm run dist` and `node scripts/cp-pkgs.js`'
);
process.exit(1);
}

let invalidMains = 0;
let invalidTypes = 0;
globSync('packages/*/package.json').forEach((jsonPath) => {
const packageInfo = JSON.parse(fs.readFileSync(jsonPath));
if (!packageInfo.main) {
return;
}

const mainPath = path.join(path.dirname(jsonPath), packageInfo.main);
let isInvalid = false;
let isTypesInvalid = false;

const mainPath = path.join(path.dirname(jsonPath), packageInfo.main || '');
if (mainPath.indexOf('dist') < 0) {
isInvalid = true;
console.error(`${jsonPath} main property does not reference a file under dist`);
console.error(
`${jsonPath} main property does not reference a file under dist`
);
}
if (!fs.existsSync(mainPath)) {
isInvalid = true;
console.error(`${jsonPath} main property points to nonexistent ${mainPath}`);
console.error(
`${jsonPath} main property points to nonexistent ${mainPath}`
);
}
const typesPath = path.join(path.dirname(jsonPath), packageInfo.types || '');
if (typesPath.indexOf('dist') < 0) {
isTypesInvalid = true;
console.error(
`${jsonPath} types property does not reference a file under dist`
);
}
if (!fs.existsSync(typesPath)) {
isTypesInvalid = true;
console.error(
`${jsonPath} types property points to nonexistent ${typesPath}`
);
}

if (isInvalid) {
// Multiple checks could have failed, but only increment the counter once for one package.
invalidMains++;
}
if (isTypesInvalid) {
invalidTypes++;
}
});

if (invalidMains > 0) {
console.error(`${invalidMains} incorrect main property values found; please fix.`);
if (invalidMains > 0 || invalidTypes > 0) {
if (invalidMains > 0) {
console.error(
`${invalidMains} incorrect main property values found; please fix.`
);
}
if (invalidTypes > 0) {
console.error(
`${invalidTypes} incorrect types property values found; please fix.`
);
}
} else {
console.log('Success: All packages with main properties reference valid files under dist!');
console.log(
'Success: All packages with main/types properties reference valid files under dist!'
);
}
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{
"extends": "gts/tsconfig-google.json",
"compilerOptions": {
"outDir": "./types/",
"outDir": "./build",
"declarationDir": "./build/types",
"sourceMap": true,
"module": "commonjs",
"lib": ["es2015", "es2017", "dom"],
"jsx": "react",
"target": "es5",
"noUnusedLocals": true,
"noUnusedParameters": true,
"esModuleInterop": true,
"esModuleInterop": true
},
"compileOnSave": true,
"exclude": [
Expand Down

0 comments on commit 3d61838

Please sign in to comment.