Skip to content

Commit

Permalink
feat(umi-build-dva): ignore test files as model, Close umijs#419
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed May 9, 2018
1 parent 6a4c2e7 commit 6f73dcc
Show file tree
Hide file tree
Showing 19 changed files with 120 additions and 31 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
'/lib/',
'/packages/umi/src/scripts/test.js',
'/packages/umi/src/test.js',
'/packages/**/fixtures/**/*.test.(j|t)s?(x)',
],
collectCoverageFrom: ['packages/**/src/**/*.{ts,tsx,js,jsx}'],
};
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@


Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@


Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@


Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@


Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@


Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
70 changes: 40 additions & 30 deletions packages/umi-plugin-dva/src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,45 @@
import { readFileSync, writeFileSync, existsSync, statSync } from 'fs';
import { readFileSync, writeFileSync, existsSync } from 'fs';
import { join, dirname } from 'path';
import globby from 'globby';
import uniq from 'lodash.uniq';

export function getModel(cwd, api) {
const { config } = api.service;
const { winPath } = api.utils;

const modelJSPath = join(cwd, 'model.js');
if (existsSync(modelJSPath)) {
return [winPath(modelJSPath)];
}
const modelJSXPath = join(cwd, 'model.jsx');
if (existsSync(modelJSXPath)) {
return [winPath(modelJSXPath)];
}
const modelTSPath = join(cwd, 'model.ts');
if (existsSync(modelTSPath)) {
return [winPath(modelTSPath)];
}

const modelTSXPath = join(cwd, 'model.tsx');
if (existsSync(modelTSXPath)) {
return [winPath(modelTSXPath)];
}

return globby
.sync(`./${config.singular ? 'model' : 'models'}/**/*.{ts,tsx,js,jsx}`, {
cwd,
})
.filter(
p =>
!p.endsWith('.d.ts') &&
!p.endsWith('.test.js') &&
!p.endsWith('.test.jsx') &&
!p.endsWith('.test.ts') &&
!p.endsWith('.test.tsx'),
)
.map(p => winPath(join(cwd, p)));
}

export default function(api, opts = {}) {
const { RENDER, ROUTER_MODIFIER, IMPORT } = api.placeholder;
const { paths, config } = api.service;
Expand All @@ -20,33 +57,6 @@ export default function(api, opts = {}) {
}
}

function getModel(cwd) {
const modelJSPath = join(cwd, 'model.js');
if (existsSync(modelJSPath)) {
return [winPath(modelJSPath)];
}
const modelJSXPath = join(cwd, 'model.jsx');
if (existsSync(modelJSXPath)) {
return [winPath(modelJSXPath)];
}
const modelTSPath = join(cwd, 'model.ts');
if (existsSync(modelTSPath)) {
return [winPath(modelTSPath)];
}

const modelTSXPath = join(cwd, 'model.tsx');
if (existsSync(modelTSXPath)) {
return [winPath(modelTSXPath)];
}

return globby
.sync(`./${config.singular ? 'model' : 'models'}/**/*.{ts,tsx,js,jsx}`, {
cwd,
})
.filter(p => !p.endsWith('.d.ts'))
.map(p => winPath(join(cwd, p)));
}

function endWithSlash(path) {
return path.slice(-1) !== '/' ? `${path}/` : path;
}
Expand Down Expand Up @@ -74,7 +84,7 @@ export default function(api, opts = {}) {
}

function getGlobalModels() {
let models = getModel(paths.absSrcPath);
let models = getModel(paths.absSrcPath, api);
if (!shouldImportDynamic) {
// dev 模式下还需要额外载入 page 路由的 models 文件
models = [...models, ...getModelsWithRoutes(api.service.routes)];
Expand All @@ -87,7 +97,7 @@ export default function(api, opts = {}) {
function getPageModels(cwd) {
let models = [];
while (!isPagesPath(cwd) && !isSrcPath(cwd) && cwd !== '/') {
models = models.concat(getModel(cwd));
models = models.concat(getModel(cwd, api));
cwd = dirname(cwd);
}
return models;
Expand Down
64 changes: 64 additions & 0 deletions packages/umi-plugin-dva/src/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { join } from 'path';
import { getModel } from './index';

const api = {
service: {
config: {
singular: false,
},
},
utils: {
winPath(p) {
return p;
},
},
};

const base = join(__dirname, 'fixtures', 'getModel');

function normalizeModels(models, base) {
return models.map(model => model.replace(base, '$CWD$'));
}

describe('umi-plugin-dva', () => {
it('getModel with model.js', () => {
const dir = join(base, 'model');
const models = normalizeModels(getModel(dir, api), dir);
expect(models).toEqual(['$CWD$/model.js']);
});

it('getModel with models directory', () => {
const dir = join(base, 'models');
const models = normalizeModels(getModel(dir, api), dir);
expect(models).toEqual([
'$CWD$/models/a.js',
'$CWD$/models/a.jsx',
'$CWD$/models/a.ts',
'$CWD$/models/a.tsx',
]);
});

it('getModel with model directory and singular', () => {
const dir = join(base, 'models-with-singular');
const models = normalizeModels(
getModel(dir, {
...api,
service: { config: { singular: true } },
}),
dir,
);
expect(models).toEqual(['$CWD$/model/a.js']);
});

it('getModel ignore d.ts', () => {
const dir = join(base, 'ignore-d-ts');
const models = normalizeModels(getModel(dir, api), dir);
expect(models).toEqual(['$CWD$/models/a.ts']);
});

it('getModel ignore test files', () => {
const dir = join(base, 'ignore-test-files');
const models = normalizeModels(getModel(dir, api), dir);
expect(models).toEqual(['$CWD$/models/a.ts']);
});
});
6 changes: 5 additions & 1 deletion scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ function transform(opts = {}) {
function buildPkg(pkg) {
rimraf.sync(join(cwd, 'packages', pkg, 'lib'));
const stream = vfs
.src(`./packages/${pkg}/src/**/*.js`)
.src([
`./packages/${pkg}/src/**/*.js`,
`!./packages/${pkg}/src/**/fixtures/**/*.js`,
`!./packages/${pkg}/src/**/*.test.js`,
])
.pipe(
through.obj((f, enc, cb) => {
f.contents = new Buffer( // eslint-disable-line
Expand Down

0 comments on commit 6f73dcc

Please sign in to comment.