Insert License Header

Insert a license header into a source code string.

Usage

var insertHeader = require( '@stdlib/_tools/licenses/insert-header' );

insertHeader( src, header )

Inserts a provided license header into a source code string (or Buffer).

var licenseHeader = require( '@stdlib/_tools/licenses/header' );

// Source code string:
var src = [
    '#!/usr/bin/env bash',
    '',
    'echo "beep"',
    ''
];
src = src.join( '\n' );

// Generate a license header:
var opts = {
    'year': 2018,
    'copyright': 'The Stdlib Authors'
};
var h = licenseHeader( 'Apache-2.0', 'bash', opts );

// Insert the header:
src = insertHeader( src, h );
#!/usr/bin/env bash
#
# @license Apache-2.0
#
# Copyright (c) 2018 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.

echo "beep"

If provided a source code Buffer, the function returns a Buffer.

var string2buffer = require( '@stdlib/buffer/from-string' );
var licenseHeader = require( '@stdlib/_tools/licenses/header' );

// Source code buffer:
var src = [
    '#!/usr/bin/env bash',
    '',
    'echo "beep"',
    ''
];
src = string2buffer( src.join( '\n' ) );

// Generate a license header:
var opts = {
    'year': 2018,
    'copyright': 'The Stdlib Authors'
};
var h = licenseHeader( 'Apache-2.0', 'bash', opts );

// Insert the header:
src = insertHeader( src, h );
// returns <Buffer>
#!/usr/bin/env bash
#
# @license Apache-2.0
#
# Copyright (c) 2018 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.

echo "beep"

Examples

var join = require( 'path' ).join;
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
var licenseHeader = require( '@stdlib/_tools/licenses/header' );
var insertHeader = require( '@stdlib/_tools/licenses/insert-header' );

// Generate a license header:
var opts = {
    'year': 2018,
    'copyright': 'The Stdlib Authors'
};

var h = licenseHeader( 'Apache-2.0', 'fortran', opts );

// Load a Fortran file:
var fopts = {
    'encoding': 'utf8'
};

var fpath = join( __dirname, 'examples', 'fixtures', 'file.f.txt' );
var file = readFileSync( fpath, fopts );

// Insert the license header:
file = insertHeader( file, h );
// returns <string>