chore(doclint): remove SourceFactory (#2447)

SourceFactory was meant to cache Sources so that they could be used
in different preprocessor tasks.

This turned out to be over-engineering. This patch kills the layer.
This commit is contained in:
Andrey Lushnikov 2018-04-25 18:07:20 -07:00 committed by GitHub
parent 6d19db4df1
commit 13a41495aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 66 deletions

View File

@ -82,44 +82,19 @@ class Source {
hasUpdatedText() { hasUpdatedText() {
return this._hasUpdatedText; return this._hasUpdatedText;
} }
}
class SourceFactory { async save() {
constructor() { await writeFileAsync(this.filePath(), this.text());
this._sources = new Map();
}
/**
* @return {!Array<!Source>}
*/
sources() {
return Array.from(this._sources.values());
}
/**
* @return {!Promise<boolean>}
*/
async saveChangedSources() {
const changedSources = Array.from(this._sources.values()).filter(source => source.hasUpdatedText());
if (!changedSources.length)
return false;
await Promise.all(changedSources.map(source => writeFileAsync(source.filePath(), source.text())));
return true;
} }
/** /**
* @param {string} filePath * @param {string} filePath
* @return {!Promise<Source>} * @return {!Promise<Source>}
*/ */
async readFile(filePath) { static async readFile(filePath) {
filePath = path.resolve(filePath); filePath = path.resolve(filePath);
let source = this._sources.get(filePath);
if (!source) {
const text = await readFileAsync(filePath, {encoding: 'utf8'}); const text = await readFileAsync(filePath, {encoding: 'utf8'});
source = new Source(filePath, text); return new Source(filePath, text);
this._sources.set(filePath, source);
}
return source;
} }
/** /**
@ -127,21 +102,13 @@ class SourceFactory {
* @param {string=} extension * @param {string=} extension
* @return {!Promise<!Array<!Source>>} * @return {!Promise<!Array<!Source>>}
*/ */
async readdir(dirPath, extension = '') { static async readdir(dirPath, extension = '') {
const fileNames = await readdirAsync(dirPath); const fileNames = await readdirAsync(dirPath);
const filePaths = fileNames.filter(fileName => fileName.endsWith(extension)).map(fileName => path.join(dirPath, fileName)); const filePaths = fileNames.filter(fileName => fileName.endsWith(extension)).map(fileName => path.join(dirPath, fileName));
return Promise.all(filePaths.map(filePath => this.readFile(filePath))); return Promise.all(filePaths.map(filePath => Source.readFile(filePath)));
}
/**
* @param {string} filePath
* @param {string} text
* @return {!Source}
*/
createForTest(filePath, text) {
return new Source(filePath, text);
} }
} }
module.exports = Source;
/** /**
* @param {function(?)} nodeFunction * @param {function(?)} nodeFunction
@ -166,4 +133,3 @@ function promisify(nodeFunction) {
}; };
} }
module.exports = SourceFactory;

View File

@ -17,7 +17,7 @@
const path = require('path'); const path = require('path');
const puppeteer = require('../../../..'); const puppeteer = require('../../../..');
const checkPublicAPI = require('..'); const checkPublicAPI = require('..');
const SourceFactory = require('../../SourceFactory'); const Source = require('../../Source');
const mdBuilder = require('../MDBuilder'); const mdBuilder = require('../MDBuilder');
const jsBuilder = require('../JSBuilder'); const jsBuilder = require('../JSBuilder');
const GoldenUtils = require('../../../../test/golden-utils'); const GoldenUtils = require('../../../../test/golden-utils');
@ -64,9 +64,8 @@ async function testLint(state, test) {
toBeGolden: GoldenUtils.compare.bind(null, dirPath, dirPath) toBeGolden: GoldenUtils.compare.bind(null, dirPath, dirPath)
}); });
const factory = new SourceFactory(); const mdSources = await Source.readdir(dirPath, '.md');
const mdSources = await factory.readdir(dirPath, '.md'); const jsSources = await Source.readdir(dirPath, '.js');
const jsSources = await factory.readdir(dirPath, '.js');
const messages = await checkPublicAPI(page, mdSources, jsSources); const messages = await checkPublicAPI(page, mdSources, jsSources);
const errors = messages.map(message => message.text); const errors = messages.map(message => message.text);
expect(errors.join('\n')).toBeGolden('result.txt'); expect(errors.join('\n')).toBeGolden('result.txt');
@ -77,8 +76,7 @@ async function testMDBuilder(state, test) {
const {expect} = new Matchers({ const {expect} = new Matchers({
toBeGolden: GoldenUtils.compare.bind(null, dirPath, dirPath) toBeGolden: GoldenUtils.compare.bind(null, dirPath, dirPath)
}); });
const factory = new SourceFactory(); const sources = await Source.readdir(dirPath, '.md');
const sources = await factory.readdir(dirPath, '.md');
const {documentation} = await mdBuilder(page, sources); const {documentation} = await mdBuilder(page, sources);
expect(serialize(documentation)).toBeGolden('result.txt'); expect(serialize(documentation)).toBeGolden('result.txt');
} }
@ -88,8 +86,7 @@ async function testJSBuilder(state, test) {
const {expect} = new Matchers({ const {expect} = new Matchers({
toBeGolden: GoldenUtils.compare.bind(null, dirPath, dirPath) toBeGolden: GoldenUtils.compare.bind(null, dirPath, dirPath)
}); });
const factory = new SourceFactory(); const sources = await Source.readdir(dirPath, '.js');
const sources = await factory.readdir(dirPath, '.js');
const {documentation} = await jsBuilder(sources); const {documentation} = await jsBuilder(sources);
expect(serialize(documentation)).toBeGolden('result.txt'); expect(serialize(documentation)).toBeGolden('result.txt');
} }

View File

@ -17,7 +17,7 @@
const puppeteer = require('../..'); const puppeteer = require('../..');
const path = require('path'); const path = require('path');
const SourceFactory = require('./SourceFactory'); const Source = require('./Source');
const PROJECT_DIR = path.join(__dirname, '..', '..'); const PROJECT_DIR = path.join(__dirname, '..', '..');
const VERSION = require(path.join(PROJECT_DIR, 'package.json')).version; const VERSION = require(path.join(PROJECT_DIR, 'package.json')).version;
@ -31,13 +31,13 @@ run();
async function run() { async function run() {
const startTime = Date.now(); const startTime = Date.now();
const sourceFactory = new SourceFactory();
/** @type {!Array<!Message>} */ /** @type {!Array<!Message>} */
const messages = []; const messages = [];
let changedFiles = false;
// Documentation checks. // Documentation checks.
{ {
const apiSource = await sourceFactory.readFile(path.join(PROJECT_DIR, 'docs', 'api.md')); const apiSource = await Source.readFile(path.join(PROJECT_DIR, 'docs', 'api.md'));
const mdSources = [apiSource]; const mdSources = [apiSource];
const toc = require('./toc'); const toc = require('./toc');
@ -49,9 +49,16 @@ async function run() {
const browser = await puppeteer.launch({args: ['--no-sandbox']}); const browser = await puppeteer.launch({args: ['--no-sandbox']});
const page = await browser.newPage(); const page = await browser.newPage();
const checkPublicAPI = require('./check_public_api'); const checkPublicAPI = require('./check_public_api');
const jsSources = await sourceFactory.readdir(path.join(PROJECT_DIR, 'lib'), '.js'); const jsSources = await Source.readdir(path.join(PROJECT_DIR, 'lib'), '.js');
messages.push(...await checkPublicAPI(page, mdSources, jsSources)); messages.push(...await checkPublicAPI(page, mdSources, jsSources));
await browser.close(); await browser.close();
for (const source of mdSources) {
if (!source.hasUpdatedText())
continue;
await source.save();
changedFiles = true;
}
} }
// Report results. // Report results.
@ -74,7 +81,7 @@ async function run() {
} }
} }
let clearExit = messages.length === 0; let clearExit = messages.length === 0;
if (await sourceFactory.saveChangedSources()) { if (changedFiles) {
if (clearExit) if (clearExit)
console.log(`${YELLOW_COLOR}Some files were updated.${RESET_COLOR}`); console.log(`${YELLOW_COLOR}Some files were updated.${RESET_COLOR}`);
clearExit = false; clearExit = false;

View File

@ -15,9 +15,7 @@
*/ */
const preprocessor = require('.'); const preprocessor = require('.');
const SourceFactory = require('../SourceFactory'); const Source = require('../Source');
const factory = new SourceFactory();
const {TestRunner, Reporter, Matchers} = require('../../testrunner/'); const {TestRunner, Reporter, Matchers} = require('../../testrunner/');
const runner = new TestRunner(); const runner = new TestRunner();
new Reporter(runner); new Reporter(runner);
@ -29,7 +27,7 @@ const {expect} = new Matchers();
describe('preprocessor', function() { describe('preprocessor', function() {
it('should throw for unknown command', function() { it('should throw for unknown command', function() {
const source = factory.createForTest('doc.md', ` const source = new Source('doc.md', `
<!-- gen:unknown-command -->something<!-- gen:stop --> <!-- gen:unknown-command -->something<!-- gen:stop -->
`); `);
const messages = preprocessor([source], '1.1.1'); const messages = preprocessor([source], '1.1.1');
@ -40,7 +38,7 @@ describe('preprocessor', function() {
}); });
describe('gen:version', function() { describe('gen:version', function() {
it('should work', function() { it('should work', function() {
const source = factory.createForTest('doc.md', ` const source = new Source('doc.md', `
Puppeteer <!-- gen:version -->XXX<!-- gen:stop --> Puppeteer <!-- gen:version -->XXX<!-- gen:stop -->
`); `);
const messages = preprocessor([source], '1.2.0'); const messages = preprocessor([source], '1.2.0');
@ -52,7 +50,7 @@ describe('preprocessor', function() {
`); `);
}); });
it('should work for *-post versions', function() { it('should work for *-post versions', function() {
const source = factory.createForTest('doc.md', ` const source = new Source('doc.md', `
Puppeteer <!-- gen:version -->XXX<!-- gen:stop --> Puppeteer <!-- gen:version -->XXX<!-- gen:stop -->
`); `);
const messages = preprocessor([source], '1.2.0-post'); const messages = preprocessor([source], '1.2.0-post');
@ -64,13 +62,13 @@ describe('preprocessor', function() {
`); `);
}); });
it('should tolerate different writing', function() { it('should tolerate different writing', function() {
const source = factory.createForTest('doc.md', `Puppeteer v<!-- gEn:version -->WHAT const source = new Source('doc.md', `Puppeteer v<!-- gEn:version -->WHAT
<!-- GEN:stop -->`); <!-- GEN:stop -->`);
preprocessor([source], '1.1.1'); preprocessor([source], '1.1.1');
expect(source.text()).toBe(`Puppeteer v<!-- gEn:version -->v1.1.1<!-- GEN:stop -->`); expect(source.text()).toBe(`Puppeteer v<!-- gEn:version -->v1.1.1<!-- GEN:stop -->`);
}); });
it('should not tolerate missing gen:stop', function() { it('should not tolerate missing gen:stop', function() {
const source = factory.createForTest('doc.md', `<!--GEN:version-->`); const source = new Source('doc.md', `<!--GEN:version-->`);
const messages = preprocessor([source], '1.2.0'); const messages = preprocessor([source], '1.2.0');
expect(source.hasUpdatedText()).toBe(false); expect(source.hasUpdatedText()).toBe(false);
expect(messages.length).toBe(1); expect(messages.length).toBe(1);
@ -80,7 +78,7 @@ describe('preprocessor', function() {
}); });
describe('gen:empty-if-release', function() { describe('gen:empty-if-release', function() {
it('should clear text when release version', function() { it('should clear text when release version', function() {
const source = factory.createForTest('doc.md', ` const source = new Source('doc.md', `
<!-- gen:empty-if-release -->XXX<!-- gen:stop --> <!-- gen:empty-if-release -->XXX<!-- gen:stop -->
`); `);
const messages = preprocessor([source], '1.1.1'); const messages = preprocessor([source], '1.1.1');
@ -92,7 +90,7 @@ describe('preprocessor', function() {
`); `);
}); });
it('should keep text when non-release version', function() { it('should keep text when non-release version', function() {
const source = factory.createForTest('doc.md', ` const source = new Source('doc.md', `
<!-- gen:empty-if-release -->XXX<!-- gen:stop --> <!-- gen:empty-if-release -->XXX<!-- gen:stop -->
`); `);
const messages = preprocessor([source], '1.1.1-post'); const messages = preprocessor([source], '1.1.1-post');
@ -103,7 +101,7 @@ describe('preprocessor', function() {
}); });
}); });
it('should work with multiple commands', function() { it('should work with multiple commands', function() {
const source = factory.createForTest('doc.md', ` const source = new Source('doc.md', `
<!-- gen:version -->XXX<!-- gen:stop --> <!-- gen:version -->XXX<!-- gen:stop -->
<!-- gen:empty-if-release -->YYY<!-- gen:stop --> <!-- gen:empty-if-release -->YYY<!-- gen:stop -->
<!-- gen:version -->ZZZ<!-- gen:stop --> <!-- gen:version -->ZZZ<!-- gen:stop -->