#!/usr/bin/env node

/**
* @license Apache-2.0
*
* Copyright (c) 2021 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var join = require( 'path' ).join;
var mkdir = require( 'fs' ).mkdirSync;
var exists = require( '@stdlib/fs/exists' ).sync;
var writeFile = require( '@stdlib/fs/write-file' ).sync;
var namespaces = require( '@stdlib/_tools/pkgs/namespaces' ).sync;
var documentationPath = require( './../utils/api_docs_path.js' );


// VARIABLES //

var OUTPUT = 'namespace_list.json';


// MAIN //

/**
* Main execution sequence.
*
* @private
* @throws {Error} unexpected error
*/
function main() {
	var opts;
	var list;
	var dir;
	var i;

	dir = documentationPath();
	if ( !exists( dir ) ) {
		mkdir( dir );
	}
	// Resolve the list of namespaces:
	opts = {
		'ignore': [
			'**/_tools/**'
		]
	};
	list = namespaces( opts );

	// Remove the `@stdlib/` prefix...
	for ( i = 0; i < list.length; i++ ) {
		list[ i ] = list[ i ].substring( 8 ); // '@stdlib/' is 8 characters long
	}
	// Save as JSON file:
	opts = {
		'encoding': 'utf8'
	};
	writeFile( join( dir, OUTPUT ), JSON.stringify( list ), opts );
}

main();

@@ -705,53 +705,17 @@
 var 
-mkdir = require( 'fs' ).mkdirSync;%0Avar exists
+writeFile
  = r
@@ -734,22 +734,26 @@
 dlib/fs/
-exists
+write-file
 ' ).sync
@@ -754,33 +754,32 @@
 ).sync;%0Avar 
-writeFile
+readJSON
  = require( 
@@ -786,34 +786,33 @@
 '@stdlib/fs/
-write-file
+read-json
 ' ).sync;%0Ava
@@ -1111,16 +1111,28 @@
 ain() %7B%0A
+%09var order;%0A
 %09var opt
@@ -1167,81 +1167,281 @@
  i;%0A
-%0A%09dir = documentationPath();%0A%09if ( !exists( dir ) ) %7B%0A%09%09mkdir( dir )
+%09var j;%0A%0A%09// Resolve the API documentation path:%0A%09dir = documentationPath();%0A%0A%09// Load the API documentation package order:%0A%09opts = %7B%0A%09%09'encoding': 'utf8'%0A%09%7D;%0A%09order = readJSON( join( dir, 'package_order.json' ), opts );%0A%09if ( order instanceof Error ) %7B%0A%09%09throw order
 ;%0A%09%7D%0A
+%0A
 %09// 
@@ -1559,36 +1559,34 @@
 %09// 
-Remove the %60@stdlib/%60 prefix
+Convert to a look-up table
 ...%0A
@@ -1630,19 +1630,18 @@
 %7B%0A%09%09
-list%5B i %5D =
+j = order%5B
  lis
@@ -1661,16 +1661,18 @@
 ing( 8 )
+ %5D
 ; // '@s
@@ -1678,16 +1678,23 @@
 stdlib/'
+ prefix
  is 8 ch
@@ -1707,16 +1707,198 @@
 rs long%0A
+%09%09if ( j === void 0 ) %7B%0A%09%09%09throw new Error( 'unexpected error. Unable to find package %60' + list%5B i %5D + '%60 in the package order. The data may be out-of-sync.' );%0A%09%09%7D%0A%09%09list%5B i %5D = j;%0A
 %09%7D%0A%09// S

Refactor to use package index, rather than name, in order to save bytes