diff --git a/docgen/package-lock.json b/docgen/package-lock.json index fed11092a6a..d468229e44b 100644 --- a/docgen/package-lock.json +++ b/docgen/package-lock.json @@ -1523,6 +1523,21 @@ "integrity": "sha1-wcS5vuPglyXdsQa3XB4wH+LxiyY=", "dev": true }, + "filepath": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/filepath/-/filepath-1.1.0.tgz", + "integrity": "sha1-173JSJbZdS+o+iN+J16emOYwmO0=", + "requires": { + "bluebird": "3.0.5" + }, + "dependencies": { + "bluebird": { + "version": "3.0.5", + "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.0.5.tgz", + "integrity": "sha1-L/nQfJs+2ynW0oD+B1KDZefs05I=" + } + } + }, "fileset": { "version": "https://registry.npmjs.org/fileset/-/fileset-2.0.3.tgz", "integrity": "sha1-jnVIqW08wjJ+5eZ0FocjozO7oqA=", @@ -1638,19 +1653,13 @@ } }, "fs-extra": { - "version": "https://registry.npmjs.org/fs-extra/-/fs-extra-0.6.4.tgz", - "integrity": "sha1-9G8MdbeEH40gCzNIzU1pHVoJnRU=", + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-4.0.2.tgz", + "integrity": "sha1-+RcExT0bRh+JNFKwwwfZmXZHq2s=", "requires": { - "jsonfile": "https://registry.npmjs.org/jsonfile/-/jsonfile-1.0.1.tgz", - "mkdirp": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.5.tgz", - "ncp": "https://registry.npmjs.org/ncp/-/ncp-0.4.2.tgz", - "rimraf": "https://registry.npmjs.org/rimraf/-/rimraf-2.2.8.tgz" - }, - "dependencies": { - "mkdirp": { - "version": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.5.tgz", - "integrity": "sha1-3j5fiWHIjHh+4TaN+EmsRBPsqNc=" - } + "graceful-fs": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.11.tgz", + "jsonfile": "4.0.0", + "universalify": "0.1.1" } }, "fs-then-native": { @@ -1660,21 +1669,6 @@ "feature-detect-es6": "https://registry.npmjs.org/feature-detect-es6/-/feature-detect-es6-1.3.1.tgz" } }, - "fs.extra": { - "version": "https://registry.npmjs.org/fs.extra/-/fs.extra-1.3.2.tgz", - "integrity": "sha1-3QI/kwE77iRTHxszUUw3sg/ZM0k=", - "requires": { - "fs-extra": "https://registry.npmjs.org/fs-extra/-/fs-extra-0.6.4.tgz", - "mkdirp": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.5.tgz", - "walk": "https://registry.npmjs.org/walk/-/walk-2.3.9.tgz" - }, - "dependencies": { - "mkdirp": { - "version": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.5.tgz", - "integrity": "sha1-3j5fiWHIjHh+4TaN+EmsRBPsqNc=" - } - } - }, "fs.realpath": { "version": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" @@ -2830,8 +2824,12 @@ "integrity": "sha1-Hq3nrMASA0rYTiOWdn6tn6VJWCE=" }, "jsonfile": { - "version": "https://registry.npmjs.org/jsonfile/-/jsonfile-1.0.1.tgz", - "integrity": "sha1-6l7+QLg2kLmGZ2FKc5L8YOhCwN0=" + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz", + "integrity": "sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=", + "requires": { + "graceful-fs": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.11.tgz" + } }, "jsonify": { "version": "https://registry.npmjs.org/jsonify/-/jsonify-0.0.0.tgz", @@ -3218,14 +3216,6 @@ } } }, - "mkdirp-promise": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/mkdirp-promise/-/mkdirp-promise-5.0.1.tgz", - "integrity": "sha1-6bj2jlUsaKnBcTuEiD96HdA5uKE=", - "requires": { - "mkdirp": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz" - } - }, "modify-values": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/modify-values/-/modify-values-1.0.0.tgz", @@ -3246,10 +3236,6 @@ "minimatch": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz" } }, - "ncp": { - "version": "https://registry.npmjs.org/ncp/-/ncp-0.4.2.tgz", - "integrity": "sha1-q8xsvT7C7Spyn/bnwfqPAXhKhXQ=" - }, "nerf-dart": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/nerf-dart/-/nerf-dart-1.0.0.tgz", @@ -4061,10 +4047,6 @@ "align-text": "https://registry.npmjs.org/align-text/-/align-text-0.1.4.tgz" } }, - "rimraf": { - "version": "https://registry.npmjs.org/rimraf/-/rimraf-2.2.8.tgz", - "integrity": "sha1-5Dm+Kq7jJzIZUnMPmaiSnk/FBYI=" - }, "run-auto": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/run-auto/-/run-auto-2.0.0.tgz", @@ -4767,6 +4749,11 @@ "resolved": "https://registry.npmjs.org/underscore.string/-/underscore.string-2.2.1.tgz", "integrity": "sha1-18D6KvXVoaZ/QlPa7pgTLnM/Dxk=" }, + "universalify": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.1.tgz", + "integrity": "sha1-+nG63UQ3r0wUiEHjs7Fl+enlkLc=" + }, "util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", diff --git a/docgen/package.json b/docgen/package.json index 791cb9f31d3..bf54c1d8c50 100644 --- a/docgen/package.json +++ b/docgen/package.json @@ -10,8 +10,9 @@ "connect": "2.8.3", "deep-assign": "^2.0.0", "feed": "^0.3.0", + "filepath": "^1.1.0", "flow-parser": "^0.32.0", - "fs.extra": "1.3.2", + "fs-extra": "^4.0.2", "glob": "6.0.4", "glob-promise": "^3.2.0", "jsdoc-api": "^1.1.0", diff --git a/docgen/server/docsList.js b/docgen/server/docsList.js index 8cfc84fd733..14ce889c6b5 100644 --- a/docgen/server/docsList.js +++ b/docgen/server/docsList.js @@ -10,101 +10,101 @@ 'use strict'; const components = [ - '../Libraries/Components/ActivityIndicator/ActivityIndicator.js', - '../Libraries/Components/Button.js', - '../Libraries/Components/DatePicker/DatePickerIOS.ios.js', - '../Libraries/Components/DrawerAndroid/DrawerLayoutAndroid.android.js', - '../Libraries/Lists/FlatList.js', - '../Libraries/Image/Image.ios.js', - '../Libraries/Components/Keyboard/KeyboardAvoidingView.js', - '../Libraries/Lists/ListView/ListView.js', - '../Libraries/Components/MaskedView/MaskedViewIOS.ios.js', - '../Libraries/Modal/Modal.js', - '../Libraries/Components/Navigation/NavigatorIOS.ios.js', - '../Libraries/Components/Picker/Picker.js', - '../Libraries/Components/Picker/PickerIOS.ios.js', - '../Libraries/Components/ProgressBarAndroid/ProgressBarAndroid.android.js', - '../Libraries/Components/ProgressViewIOS/ProgressViewIOS.ios.js', - '../Libraries/Components/RefreshControl/RefreshControl.js', - '../Libraries/Components/ScrollView/ScrollView.js', - '../Libraries/Lists/SectionList.js', - '../Libraries/Components/SegmentedControlIOS/SegmentedControlIOS.ios.js', - '../Libraries/Components/Slider/Slider.js', - '../Libraries/RCTTest/SnapshotViewIOS.ios.js', - '../Libraries/Components/StatusBar/StatusBar.js', - '../Libraries/Components/Switch/Switch.js', - '../Libraries/Components/TabBarIOS/TabBarIOS.ios.js', - '../Libraries/Components/TabBarIOS/TabBarItemIOS.ios.js', - '../Libraries/Text/Text.js', - '../Libraries/Components/TextInput/TextInput.js', - '../Libraries/Components/ToolbarAndroid/ToolbarAndroid.android.js', - '../Libraries/Components/Touchable/TouchableHighlight.js', - '../Libraries/Components/Touchable/TouchableNativeFeedback.android.js', - '../Libraries/Components/Touchable/TouchableOpacity.js', - '../Libraries/Components/Touchable/TouchableWithoutFeedback.js', - '../Libraries/Components/View/View.js', - '../Libraries/Components/ViewPager/ViewPagerAndroid.android.js', - '../Libraries/Lists/VirtualizedList.js', - '../Libraries/Components/WebView/WebView.ios.js', + 'build/Libraries/Components/ActivityIndicator/ActivityIndicator.js', + 'build/Libraries/Components/Button.js', + 'build/Libraries/Components/DatePicker/DatePickerIOS.ios.js', + 'build/Libraries/Components/DrawerAndroid/DrawerLayoutAndroid.android.js', + 'build/Libraries/Lists/FlatList.js', + 'build/Libraries/Image/Image.ios.js', + 'build/Libraries/Components/Keyboard/KeyboardAvoidingView.js', + 'build/Libraries/Lists/ListView/ListView.js', + 'build/Libraries/Components/MaskedView/MaskedViewIOS.ios.js', + 'build/Libraries/Modal/Modal.js', + 'build/Libraries/Components/Navigation/NavigatorIOS.ios.js', + 'build/Libraries/Components/Picker/Picker.js', + 'build/Libraries/Components/Picker/PickerIOS.ios.js', + 'build/Libraries/Components/ProgressBarAndroid/ProgressBarAndroid.android.js', + 'build/Libraries/Components/ProgressViewIOS/ProgressViewIOS.ios.js', + 'build/Libraries/Components/RefreshControl/RefreshControl.js', + 'build/Libraries/Components/ScrollView/ScrollView.js', + 'build/Libraries/Lists/SectionList.js', + 'build/Libraries/Components/SegmentedControlIOS/SegmentedControlIOS.ios.js', + 'build/Libraries/Components/Slider/Slider.js', + 'build/Libraries/RCTTest/SnapshotViewIOS.ios.js', + 'build/Libraries/Components/StatusBar/StatusBar.js', + 'build/Libraries/Components/Switch/Switch.js', + 'build/Libraries/Components/TabBarIOS/TabBarIOS.ios.js', + 'build/Libraries/Components/TabBarIOS/TabBarItemIOS.ios.js', + 'build/Libraries/Text/Text.js', + 'build/Libraries/Components/TextInput/TextInput.js', + 'build/Libraries/Components/ToolbarAndroid/ToolbarAndroid.android.js', + 'build/Libraries/Components/Touchable/TouchableHighlight.js', + 'build/Libraries/Components/Touchable/TouchableNativeFeedback.android.js', + 'build/Libraries/Components/Touchable/TouchableOpacity.js', + 'build/Libraries/Components/Touchable/TouchableWithoutFeedback.js', + 'build/Libraries/Components/View/View.js', + 'build/Libraries/Components/ViewPager/ViewPagerAndroid.android.js', + 'build/Libraries/Lists/VirtualizedList.js', + 'build/Libraries/Components/WebView/WebView.ios.js', ]; const apis = [ - '../Libraries/Components/AccessibilityInfo/AccessibilityInfo.ios.js', - '../Libraries/ActionSheetIOS/ActionSheetIOS.js', - '../Libraries/Alert/Alert.js', - '../Libraries/Alert/AlertIOS.js', - '../Libraries/Animated/src/AnimatedImplementation.js', - '../Libraries/ReactNative/AppRegistry.js', - '../Libraries/AppState/AppState.js', - '../Libraries/Storage/AsyncStorage.js', - '../Libraries/Utilities/BackAndroid.js', - '../Libraries/Utilities/BackHandler.ios.js', - '../Libraries/Utilities/BackHandler.android.js', - '../Libraries/CameraRoll/CameraRoll.js', - '../Libraries/Components/Clipboard/Clipboard.js', - '../Libraries/Components/DatePickerAndroid/DatePickerAndroid.android.js', - '../Libraries/Utilities/Dimensions.js', - '../Libraries/Animated/src/Easing.js', - '../Libraries/Geolocation/Geolocation.js', - '../Libraries/Image/ImageEditor.js', - '../Libraries/CameraRoll/ImagePickerIOS.js', - '../Libraries/Image/ImageStore.js', - '../Libraries/Interaction/InteractionManager.js', - '../Libraries/Components/Keyboard/Keyboard.js', - '../Libraries/LayoutAnimation/LayoutAnimation.js', - '../Libraries/Linking/Linking.js', - '../Libraries/Lists/ListView/ListViewDataSource.js', - '../Libraries/Network/NetInfo.js', - '../Libraries/Interaction/PanResponder.js', - '../Libraries/PermissionsAndroid/PermissionsAndroid.js', - '../Libraries/Utilities/PixelRatio.js', - '../Libraries/PushNotificationIOS/PushNotificationIOS.js', - '../Libraries/Settings/Settings.ios.js', - '../Libraries/Share/Share.js', - '../Libraries/Components/StatusBar/StatusBarIOS.ios.js', - '../Libraries/StyleSheet/StyleSheet.js', - '../Libraries/Performance/Systrace.js', - '../Libraries/Components/TimePickerAndroid/TimePickerAndroid.android.js', - '../Libraries/Components/ToastAndroid/ToastAndroid.android.js', - '../Libraries/Vibration/Vibration.js', - '../Libraries/Vibration/VibrationIOS.ios.js', + 'build/Libraries/Components/AccessibilityInfo/AccessibilityInfo.ios.js', + 'build/Libraries/ActionSheetIOS/ActionSheetIOS.js', + 'build/Libraries/Alert/Alert.js', + 'build/Libraries/Alert/AlertIOS.js', + 'build/Libraries/Animated/src/AnimatedImplementation.js', + 'build/Libraries/ReactNative/AppRegistry.js', + 'build/Libraries/AppState/AppState.js', + 'build/Libraries/Storage/AsyncStorage.js', + 'build/Libraries/Utilities/BackAndroid.js', + 'build/Libraries/Utilities/BackHandler.ios.js', + 'build/Libraries/Utilities/BackHandler.android.js', + 'build/Libraries/CameraRoll/CameraRoll.js', + 'build/Libraries/Components/Clipboard/Clipboard.js', + 'build/Libraries/Components/DatePickerAndroid/DatePickerAndroid.android.js', + 'build/Libraries/Utilities/Dimensions.js', + 'build/Libraries/Animated/src/Easing.js', + 'build/Libraries/Geolocation/Geolocation.js', + 'build/Libraries/Image/ImageEditor.js', + 'build/Libraries/CameraRoll/ImagePickerIOS.js', + 'build/Libraries/Image/ImageStore.js', + 'build/Libraries/Interaction/InteractionManager.js', + 'build/Libraries/Components/Keyboard/Keyboard.js', + 'build/Libraries/LayoutAnimation/LayoutAnimation.js', + 'build/Libraries/Linking/Linking.js', + 'build/Libraries/Lists/ListView/ListViewDataSource.js', + 'build/Libraries/Network/NetInfo.js', + 'build/Libraries/Interaction/PanResponder.js', + 'build/Libraries/PermissionsAndroid/PermissionsAndroid.js', + 'build/Libraries/Utilities/PixelRatio.js', + 'build/Libraries/PushNotificationIOS/PushNotificationIOS.js', + 'build/Libraries/Settings/Settings.ios.js', + 'build/Libraries/Share/Share.js', + 'build/Libraries/Components/StatusBar/StatusBarIOS.ios.js', + 'build/Libraries/StyleSheet/StyleSheet.js', + 'build/Libraries/Performance/Systrace.js', + 'build/Libraries/Components/TimePickerAndroid/TimePickerAndroid.android.js', + 'build/Libraries/Components/ToastAndroid/ToastAndroid.android.js', + 'build/Libraries/Vibration/Vibration.js', + 'build/Libraries/Vibration/VibrationIOS.ios.js', ]; const stylesForEmbed = [ - '../Libraries/Components/View/ViewStylePropTypes.js', - '../Libraries/Text/TextStylePropTypes.js', - '../Libraries/Image/ImageStylePropTypes.js', + 'build/Libraries/Components/View/ViewStylePropTypes.js', + 'build/Libraries/Text/TextStylePropTypes.js', + 'build/Libraries/Image/ImageStylePropTypes.js', ]; const stylesWithPermalink = [ - '../Libraries/StyleSheet/LayoutPropTypes.js', - '../Libraries/StyleSheet/TransformPropTypes.js', - '../Libraries/Components/View/ShadowPropTypesIOS.js', - '../Libraries/Components/View/ViewPropTypes.js', + 'build/Libraries/StyleSheet/LayoutPropTypes.js', + 'build/Libraries/StyleSheet/TransformPropTypes.js', + 'build/Libraries/Components/View/ShadowPropTypesIOS.js', + 'build/Libraries/Components/View/ViewPropTypes.js', ...stylesForEmbed, ]; -const viewPropTypes = '../Libraries/Components/View/ViewPropTypes.js'; +const viewPropTypes = 'build/Libraries/Components/View/ViewPropTypes.js'; module.exports = { components, diff --git a/docgen/server/extractDocs.js b/docgen/server/extractDocs.js index a7146a41275..9568d31aaec 100644 --- a/docgen/server/extractDocs.js +++ b/docgen/server/extractDocs.js @@ -268,6 +268,10 @@ function getViewPropTypes() { } function renderComponent(filepath) { + if (!fs.existsSync(filepath)) { + // We're processing old versions of the docs that may be missing newer libraries. + return; + } try { const fileContent = fs.readFileSync(filepath); const handlers = docgen.defaultHandlers.concat([ @@ -321,7 +325,7 @@ function parseAPIJsDocFormat(filepath, fileContent) { // Parse via jsdoc-api let jsonParsed = jsdocApi.explainSync({ source: code, - configure: './jsdocs/jsdoc-conf.json' + configure: '../jsdocs/jsdoc-conf.json' }); // Clean up jsdoc-api return jsonParsed = jsonParsed.filter(i => { @@ -455,6 +459,11 @@ function getJsDocFormatType(entities) { } function renderAPI(filepath, type) { + if (!fs.existsSync(filepath)) { + // We're processing old versions of the docs that may be missing newer libraries. + return; + } + try { const fileContent = fs.readFileSync(filepath).toString(); let json = parseAPIInferred(filepath, fileContent); @@ -484,6 +493,11 @@ function renderAPI(filepath, type) { } function renderStyle(filepath) { + if (!fs.existsSync(filepath)) { + // We're processing old versions of the docs that may be missing newer libraries. + return; + } + const json = docgen.parse( fs.readFileSync(filepath), docgenHelpers.findExportedObject, diff --git a/docgen/server/generate.js b/docgen/server/generate.js index f7c42dfa7d9..1126a2b3d7f 100644 --- a/docgen/server/generate.js +++ b/docgen/server/generate.js @@ -1,5 +1,5 @@ /** - * Copyright (c) 2015-present, Facebook, Inc. + * Copyright (c) 2017-present, Facebook, Inc. * All rights reserved. * * This source code is licensed under the BSD-style license found in the @@ -8,31 +8,71 @@ */ 'use strict'; -const fetch = require("node-fetch"); -const fs = require('fs.extra'); +const fetch = require('node-fetch'); +const filepath = require('filepath'); +const fm = require('front-matter'); +const fs = require('fs-extra'); const glob = require('glob-promise'); -const jsdom = require("jsdom"); +const jsdom = require('jsdom'); const mkdirp = require('mkdirp'); const Promise = require('bluebird'); +const shell = require('shelljs'); const convert = require('./convert.js'); -const slugify = require("../core/slugify"); +const slugify = require('../core/slugify'); -const FORMAT_HTML = 'html'; -const FORMAT_MARKDOWN = 'markdown'; +const AUTODOCS_PREFIX = 'autogen_'; +const MARKDOWN_EXTENSION = 'md'; + +const GIT_USER = 'hramos'; //process.env.GIT_USER; +const GITHUB_USERNAME = 'facebook'; // process.env.GITHUB_USERNAME; +const GITHUB_REPONAME = 'react-native'; // process.env.GITHUB_REPONAME; +const remoteBranch = `https://${GIT_USER}@github.com/${GITHUB_USERNAME}/${GITHUB_REPONAME}.git`; + +const CHECKOUT_DIR = `${GITHUB_REPONAME}-docs`; +const BUILD_DIR = 'build'; +const DOCS_DIR = 'versioned_docs'; +const SIDEBAR_DIR = 'versioned_sidebars'; const { JSDOM } = jsdom; +// Start up a server. Don't forget to close the connection when done. +const server = require('./server.js'); +server.noconvert = true; + const argv = require('minimist')(process.argv.slice(2), { alias: { - 'f': 'format' + 'c': 'clean' }, default: { - 'autodocs': true, - 'format': FORMAT_HTML + 'autodocs': true } }); +function runChecks() { + if (!shell.which('git')) { + shell.echo('Sorry, this script requires git'); + shell.exit(1); + } + + if (!GIT_USER) { + shell.echo('GIT_USER undefined.'); + shell.exit(1); + } + if (!GITHUB_USERNAME) { + shell.echo('GITHUB_USERNAME undefined.'); + shell.exit(1); + } + if (!GITHUB_REPONAME) { + shell.echo('GITHUB_REPONAME undefined.'); + shell.exit(1); + } +} + +function cleanFiles() { + return fs.remove(BUILD_DIR); +} + /** * Generates documentation for a given file. * @@ -46,14 +86,13 @@ function generateAutodocForFile(file, options) { return; } + const pathToOutputDir = filepath.create(BUILD_DIR, DOCS_DIR, options.version); + console.log(`Processing ${file}`); - const targetFile = file - .replace(/^src/, 'build') + const url = 'http://localhost:8079/' + file + .replace(/^src/, '') .replace(/\.js$/, '.html'); - const targetFileServerPath = targetFile.replace(/^build\//, ''); - - const url = 'http://localhost:8079/' + targetFileServerPath; return fetch(url) .then(response => { if(response.ok) { @@ -62,21 +101,13 @@ function generateAutodocForFile(file, options) { throw new Error('Network response was not ok.'); }) .then(body => { - mkdirp.sync(targetFile.replace(new RegExp('/[^/]*$'), '')); - fs.writeFileSync(targetFile, body); - return JSDOM.fromFile(targetFile); - }) - .then(dom => { - // TODO: Generate Markdown file as needed. - // Also, do we need to generate metadata now as well, regardless of format? Should we go straight to Markdown anyway? - // Figure out where these files should be written. - const metadata = generateMarkdownFromDOM(dom); - console.log(metadata); - // if (categories[metadata.category]) { - // categories[metadata.category].push(metadata.id); - // } else { - // categories[metadata.category] = [metadata.id]; - // } + const dom = new JSDOM(body); + const markdown = generateMarkdownFromDOM(dom); + const metadata = fm(markdown); + + const pathToOutputFile = pathToOutputDir.append(`${AUTODOCS_PREFIX}${metadata.attributes.id}.${MARKDOWN_EXTENSION}`); + + return fs.outputFile(pathToOutputFile.toString(), markdown); }) .catch(error => { console.log(error); @@ -85,45 +116,86 @@ function generateAutodocForFile(file, options) { } /** - * Generates HTML or Markdown documentation. Uses the convert script to extract docs from source files. + * Generates Markdown documentation. Uses the convert script to extract docs from source files. */ -function generateAutodocs(options) { - if (options === undefined) { - options = { format: FORMAT_HTML }; - } - - // Start up a server. Don't forget to close the connection when done. - const server = require('./server.js'); +function generateAutodocs(options = { version: 'next' }) { + console.log(`Generating Markdown files from JavaScript sources for version ${options.version}.`); convert({extractDocs: true}); - server.noconvert = true; let queue = Promise.resolve(); - queue = queue.then(function() { + return Promise.resolve().then(function() { return glob('src/**/*.js'); }).then(function(files) { let p = Promise.resolve(); files.forEach(function(file) { p = p.then(function() { - return generateAutodocForFile(file); + return generateAutodocForFile(file, options); }); }); return p; }).then(function() { - console.log(`Generated ${options.format} files from JavaScript sources.`); - }).finally(function() { - server.close(); + console.log(`Generated Markdown files from JavaScript sources for version ${options.version}.`); }).catch(function(e) { console.error(e); process.exit(1); }); } +// TODO: I think we have a sync/async problem where we checked out everything before trying to process docs +function checkOutDocs() { + const pathToGitCheckout = filepath.create(BUILD_DIR, CHECKOUT_DIR); + const p = Promise.resolve(); + return p + .then(() => { + fs.ensureDir(pathToGitCheckout.toString()); + }) + .then(() => { + return fs.exists(pathToGitCheckout.append(`.git`).toString()) + }).then(gitCheckoutExists => { + if (!gitCheckoutExists) { + shell.cd(process.cwd()); + shell.cd(BUILD_DIR); + shell.cd(CHECKOUT_DIR); + shell.exec(`git init`).code !== 0; + + if (shell.exec(`git remote add origin ${remoteBranch}`).code !== 0) { + throw new Error('Error: git remote failed'); + } + } + return; + }) + .then(() => { + shell.exec(`git config core.sparsecheckout true`).code !== 0; + shell.exec(`echo "docs/*" >> .git/info/sparse-checkout`).code !== 0; + shell.exec(`echo "Libraries/*" >> .git/info/sparse-checkout`).code !== 0; + + if (shell.exec(`git fetch`).code !== 0) { + throw new Error('Error: git fetch failed'); + } + + const tags = shell.exec(`git tag --sort=version:refname -l 'v0.??.?' 'v0.?.?'`).toString().split('\n'); + tags.forEach(function(tag) { + if (shell.exec(`git checkout ${tag}`).code !== 0) { + throw new Error('Error: git checkout failed'); + } + shell.echo(`Checked out ${tag}`); + const version = tag.substring(1); + // const versionDir = `../../website/versioned_docs/${version}`; + // shell.mkdir(versionDir); + // shell.cp(`docs/*`, `${versionDir}/.`) + // processDocs(versionDir, version); + return generateAutodocs({ version }); + }) + }) +} + + // DOM FORMATTING FUNCS function bodyContentFromDOM(dom) { - const el = dom.window.document.querySelector("#componentContent"); + const el = dom.window.document.querySelector('#componentContent'); if (el) { return el.innerHTML; } else { @@ -132,11 +204,11 @@ function bodyContentFromDOM(dom) { } function componentNameFromDOM(dom) { - const el = dom.window.document.querySelector("title"); + const el = dom.window.document.querySelector('title'); if (el) { return el.innerHTML; } else { - return "Component"; + return 'Component'; } } @@ -145,7 +217,7 @@ function componentCategoryFromDOM(dom) { if (el) { return el.content; } else { - return "Components"; + return 'Components'; } } @@ -161,48 +233,50 @@ function generateMarkdownFromDOM(dom) { } const componentName = componentNameFromDOM(dom); const slug = slugify(componentName); - let category = "Components"; + let category = 'Components'; const componentCategory = componentCategoryFromDOM(dom); if (componentCategory) { category = componentCategory; } const metadata = { id: slug, category: category }; - const res = [ - "---", - "id: " + slug, - "title: " + componentName, - "category: " + category, - "permalink: docs/" + slug + ".html", - "---", + const markdown = [ + '---', + 'id: ' + slug, + 'title: ' + componentName, + // 'category: ' + category, + // 'permalink: docs/' + slug + '.html', + '---', body ] .filter(function(line) { return line; }) - .join("\n"); + .join('\n'); - // ORIGINAL - // const targetFile = "../docs/autogen_" + componentName + ".md"; - - mkdirp.sync('build/autodocs'); - const targetFile = "build/autodocs/autogen_" + componentName + ".md"; - // ORIGINAL - // mkdirp.sync(targetFile.replace(new RegExp("/[^/]*$"), "")); - console.log("Writing " + targetFile); - fs.writeFileSync(targetFile, res); - return metadata; + return markdown; } function generateMetatadaFile(categories) { - const categoriesMetadataFile = "build/sidebar-metadata.json"; - fs.writeFileSync(categoriesMetadataFile, JSON.stringify(categories)); + const categoriesMetadataFile = `${BUILD_DIR}/sidebar-metadata.json`; + return fs.outputFile(categoriesMetadataFile, JSON.stringify(categories)); } // END DOM + +if (argv.clean) { + cleanFiles(); +} + if (argv.autodocs) { - let format = argv.format; - console.log(`Generating ${format} files from JavaScript sources.`); - generateAutodocs({ format }); + runChecks(); + checkOutDocs() + .finally(function() { + server.close(); + }).catch(function(e) { + console.error(e); + process.exit(1); + }); + } module.exports = generateAutodocs; diff --git a/docgen/server/import-existing-docs.js b/docgen/server/import-existing-docs.js index 29933605aa3..765b96eb163 100644 --- a/docgen/server/import-existing-docs.js +++ b/docgen/server/import-existing-docs.js @@ -71,7 +71,6 @@ function prepareFilesystem() { * * Generate requires a server to be started up. Can we do this from one location? */ - function checkOutDocs() { shell.cd(localCheckoutDir); diff --git a/docgen/server/parsedom.js b/docgen/server/parsedom.js deleted file mode 100644 index 9f036424c9f..00000000000 --- a/docgen/server/parsedom.js +++ /dev/null @@ -1,10 +0,0 @@ -var jsdom = require('jsdom'); - -const { JSDOM } = jsdom; - -const dom = new JSDOM(`WebView
expected content
`); -const el = dom.window.document.querySelector("#componentContent"); -console.log(el.innerHTML); - -console.log(dom.window.document.querySelector("title").innerHTML); -console.log(dom.window.document.querySelector('meta[property="rn:category"]').content); \ No newline at end of file